package shop.alien.store.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.StoreOperationalActivityVO;
import shop.alien.store.service.OperationalActivityService;

/**
 * 运营活动管理控制器
 *
 * @author system
 * @since 2025-11-26
 */
@Slf4j
@Api(tags = {"商家端-运营活动管理"})
@ApiSort(10)
@CrossOrigin
@RestController
@RequestMapping("/operationalActivity")
@RequiredArgsConstructor
public class OperationalActivityController {

    private final OperationalActivityService activityService;

    @ApiOperation(value = "根据活动ID获取详情", notes = "返回活动详细信息，包括优惠券和代金券的名称、ID、数量等信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParam(name = "id", value = "活动ID", dataTypeClass = Integer.class, paramType = "path", required = true)
    @GetMapping("/detail/{id}")
    public R<StoreOperationalActivityVO> getActivityDetailById(@PathVariable("id") Integer id) {
        log.info("OperationalActivityController.getActivityDetailById: id={}", id);
        try {
            StoreOperationalActivityVO result = activityService.getActivityDetailById(id);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("OperationalActivityController.getActivityDetailById ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("分页查看运营活动列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "商户ID", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "storeName", value = "商户名称（模糊）", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "activityStatus", value = "活动状态", dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "activityName", value = "活动名称（模糊）", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "提交开始时间", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "提交结束时间", dataTypeClass = String.class, paramType = "query")
    })
    @GetMapping("/detail")
    public R<IPage<StoreOperationalActivityVO>> pageActivityDetail(
            @RequestParam(value = "storeId", required = false) Integer storeId,
            @RequestParam(value = "storeName", required = false) String storeName,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "activityType", required = false) String activityType,
            @RequestParam(value = "activityName", required = false) String activityName,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        log.info("OperationalActivityController.pageActivityDetail storeId={}, storeName={}, pageNum={}, pageSize={}, status={}, activityName={}, startTime={}, endTime={}", storeId, storeName, pageNum, pageSize, status, activityName, startTime, endTime);
        try {
            IPage<StoreOperationalActivityVO> result = activityService.pageActivityDetail(storeId, storeName, pageNum, pageSize, status, activityName, activityType, startTime, endTime);
            return R.data(result);
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("OperationalActivityController.pageActivityDetail ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("运营活动审核处理")
    @ApiOperationSupport(order = 6)
    @PostMapping("/audit")
    public R<String> auditActivity(
            @RequestParam(value = "id", required = true) Integer id,
            @RequestParam(value = "status", required = true) Integer status,
            @RequestParam(value = "approvalComments", required = false) String approvalComments) {
        log.info("OperationalActivityController.auditActivity: id={}, status={}, approvalComments={}", id, status, approvalComments);
        if (id == null) {
            return R.fail("活动ID不能为空");
        }
        if (status == null) {
            return R.fail("审核状态不能为空");
        }
        try {
            int result = activityService.auditActivity(id, status, approvalComments);
            if (result > 0) {
                return R.success("审核处理成功");
            }
            return R.fail("审核处理失败");
        } catch (Exception e) {
            log.error("OperationalActivityController.auditActivity ERROR: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

}

