package shop.alien.store.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import shop.alien.entity.result.R;
import shop.alien.entity.storePlatform.vo.StoreOperationalActivitySignupCheckVo;
import shop.alien.entity.storePlatform.vo.StoreOperationalActivityVO;
import shop.alien.entity.storePlatform.vo.StoreOperationalActivityAchievementVo;
import shop.alien.store.dto.StoreOperationalActivitySignupDto;
import shop.alien.store.dto.StoreOperationalActivityAchievementDto;
import shop.alien.store.service.StoreOperationalActivityService;
import shop.alien.store.service.StoreOperationalActivityAchievementService;

/**
 * 运营活动详情控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"用户端-运营活动详情"})
@ApiSort(11)
@CrossOrigin
@RestController
@RequestMapping("/storeOperationalActivity")
@RequiredArgsConstructor
public class StoreOperationalActivityController {

    private final StoreOperationalActivityService operationalActivityService;
    private final StoreOperationalActivityAchievementService achievementService;

    @ApiOperation("查询活动详情")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "id", value = "活动ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    @GetMapping("/detail")
    public R<StoreOperationalActivityVO> getActivityDetail(@RequestParam("id") Integer id) {
        log.info("StoreOperationalActivityController.getActivityDetail id={}", id);
        try {
            StoreOperationalActivityVO result = operationalActivityService.getActivityDetail(id);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("StoreOperationalActivityController.getActivityDetail ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("报名校验")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activityId", value = "活动ID", dataTypeClass = Integer.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    })
    @GetMapping("/signup/check")
    public R<StoreOperationalActivitySignupCheckVo> checkSignup(@RequestParam("activityId") Integer activityId,
                                                                @RequestParam("userId") Integer userId) {
        log.info("StoreOperationalActivityController.checkSignup activityId={}, userId={}", activityId, userId);
        try {
            StoreOperationalActivitySignupCheckVo result = operationalActivityService.checkSignup(activityId, userId);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("StoreOperationalActivityController.checkSignup ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("活动报名")
    @ApiOperationSupport(order = 3)
    @PostMapping("/signup")
    public R<String> signup(@RequestBody StoreOperationalActivitySignupDto dto) {
        log.info("StoreOperationalActivityController.signup dto={}", dto);
        try {
            boolean result = operationalActivityService.signup(dto);
            return result ? R.success("报名成功") : R.fail("报名失败");
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("StoreOperationalActivityController.signup ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("新增成果")
    @ApiOperationSupport(order = 4)
    @PostMapping("/achievement/add")
    public R<String> addAchievement(@RequestBody StoreOperationalActivityAchievementDto dto) {
        log.info("StoreOperationalActivityController.addAchievement dto={}", dto);
        try {
            boolean result = achievementService.addAchievement(dto);
            return result ? R.success("新增成功") : R.fail("新增失败");
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("StoreOperationalActivityController.addAchievement ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("成果详情")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "id", value = "成果ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    @GetMapping("/achievement/detail")
    public R<StoreOperationalActivityAchievementVo> getAchievementDetail(@RequestParam("id") Integer id) {
        log.info("StoreOperationalActivityController.getAchievementDetail id={}", id);
        try {
            StoreOperationalActivityAchievementVo result = achievementService.getAchievementDetail(id);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("StoreOperationalActivityController.getAchievementDetail ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("成果列表")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "activityId", value = "活动ID", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "signupId", value = "报名ID", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataTypeClass = Integer.class, paramType = "query")
    })
    @GetMapping("/achievement/list")
    public R<IPage<StoreOperationalActivityAchievementVo>> listAchievements(
            @RequestParam(value = "activityId", required = false) Integer activityId,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "signupId", required = false) Integer signupId,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        log.info("StoreOperationalActivityController.listAchievements activityId={}, userId={}, signupId={}, pageNum={}, pageSize={}",
                activityId, userId, signupId, pageNum, pageSize);
        try {
            IPage<StoreOperationalActivityAchievementVo> result =
                    achievementService.listAchievements(activityId, userId, signupId, pageNum, pageSize);
            return R.data(result);
        } catch (Exception e) {
            log.error("StoreOperationalActivityController.listAchievements ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }
}
