package com.izouma.nineth.web.mobile.v1;


import com.izouma.nineth.domain.ActivityCollection;
import com.izouma.nineth.domain.Asset;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.ActivityOrderInput;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.lock.RedisLockException;
import com.izouma.nineth.service.ActivityOrderService;
import com.izouma.nineth.utils.SecurityUtils;
import com.izouma.nineth.web.BaseController;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activityOrder/v1")
@AllArgsConstructor
public class ActivityOrderControllerV1 extends BaseController {
    private ActivityOrderService activityOrderService;


    /**
     * 获取活动要求兑换物和详情
     * @param id 活动ID
     * @return 活动
     */
    @GetMapping("/detail/{projectId}/{id}")
    public ActivityCollection activityDetail(@PathVariable Integer projectId, @PathVariable Long id) {
        final User user = currentUser();
        return activityOrderService.activityDetail(projectId, id, user);
    }



    /**
     * 活动收集 兑换
     * @param mintActivityId 兑换的活动Id
     * @param collectionId 兑换的藏品Id
     * @param assetIds 消耗的个人资产Id
     * @return 兑换结果
     */
    @PostMapping("/create")
    public Asset create(@RequestBody ActivityOrderInput input) {
        try {
            final User user = SecurityUtils.getAuthenticatedUser();
            return activityOrderService.create(user, input.getMintActivityId(), input.getProjectId(), input.getCollectionId(), input.getAssetIds());
        }
        catch (RedisLockException err) {
            throw new BusinessException("活动火爆,请稍后再试");
        }
    }
}

