package shop.alien.storeplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.storePlatform.vo.StoreOperationalActivitySignupVO;
import shop.alien.entity.storePlatform.vo.StoreOperationalActivitySignupQueryDTO;
import shop.alien.entity.storePlatform.vo.StoreOperationalActivitySignupIdDTO;
import shop.alien.storeplatform.service.OperationalActivitySignupService;

/**
 * 运营活动报名管理控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"商家端-运营活动报名管理"})
@ApiSort(11)
@CrossOrigin
@RestController
@RequestMapping("/operationalActivitySignup")
@RequiredArgsConstructor
public class OperationalActivitySignupController {

    private final OperationalActivitySignupService signupService;

    @ApiOperation("根据商户ID查询报名列表")
    @ApiOperationSupport(order = 1)
    @PostMapping("/queryList")
    public R<IPage<StoreOperationalActivitySignupVO>> querySignupList(@RequestBody StoreOperationalActivitySignupQueryDTO dto) {
        log.info("OperationalActivitySignupController.querySignupList: dto={}", dto);
        try {
            if (dto.getStoreId() == null || dto.getStoreId() <= 0) {
                return R.fail("商户ID不能为空");
            }
            Integer pageNum = dto.getPageNum() != null ? dto.getPageNum() : 1;
            Integer pageSize = dto.getPageSize() != null ? dto.getPageSize() : 10;
            IPage<StoreOperationalActivitySignupVO> result = signupService.querySignupList(
                    dto.getStoreId(), dto.getStatus(), dto.getActivityName(), pageNum, pageSize);
            return R.data(result);
        } catch (Exception e) {
            log.error("OperationalActivitySignupController.querySignupList ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("根据ID查询报名详情")
    @ApiOperationSupport(order = 2)
    @PostMapping("/queryById")
    public R<StoreOperationalActivitySignupVO> querySignupById(@RequestBody StoreOperationalActivitySignupIdDTO dto) {
        log.info("OperationalActivitySignupController.querySignupById: id={}", dto.getId());
        try {
            if (dto.getId() == null || dto.getId() <= 0) {
                return R.fail("报名ID不能为空");
            }
            StoreOperationalActivitySignupVO result = signupService.querySignupById(dto.getId());
            if (result == null) {
                return R.fail("报名记录不存在");
            }
            return R.data(result);
        } catch (Exception e) {
            log.error("OperationalActivitySignupController.querySignupById ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("审核通过")
    @ApiOperationSupport(order = 3)
    @PostMapping("/approve")
    public R<String> approveSignup(@RequestBody StoreOperationalActivitySignupIdDTO dto) {
        log.info("OperationalActivitySignupController.approveSignup: id={}", dto.getId());
        try {
            if (dto.getId() == null || dto.getId() <= 0) {
                return R.fail("报名ID不能为空");
            }
            int result = signupService.approveSignup(dto.getId());
            if (result > 0) {
                return R.success("审核通过成功");
            }
            return R.fail("审核通过失败");
        } catch (Exception e) {
            log.error("OperationalActivitySignupController.approveSignup ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("审核拒绝")
    @ApiOperationSupport(order = 4)
    @PostMapping("/reject")
    public R<String> rejectSignup(@RequestBody StoreOperationalActivitySignupIdDTO dto) {
        log.info("OperationalActivitySignupController.rejectSignup: id={}, rejectReason={}", dto.getId(), dto.getRejectReason());
        try {
            if (dto.getId() == null || dto.getId() <= 0) {
                return R.fail("报名ID不能为空");
            }
            int result = signupService.rejectSignup(dto.getId(), dto.getRejectReason());
            if (result > 0) {
                return R.success("审核拒绝成功");
            }
            return R.fail("审核拒绝失败");
        } catch (Exception e) {
            log.error("OperationalActivitySignupController.rejectSignup ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }
}

