package com.snack.server.module.seckill.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.server.common.Result;
import com.snack.server.module.seckill.dto.req.SeckillActivitySaveReq;
import com.snack.server.module.seckill.dto.req.SeckillProductAddReq;
import com.snack.server.module.seckill.entity.SeckillActivity;
import com.snack.server.module.seckill.service.SeckillService;
import com.snack.server.module.seckill.vo.SeckillActivityVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 抢购管理（管理端）
 */
@Tag(name = "抢购管理")
@RestController
@RequestMapping("/api/admin/seckill")
@RequiredArgsConstructor
public class SeckillAdminController {

    private final SeckillService seckillService;

    @Operation(summary = "活动分页")
    @SaCheckLogin
    @GetMapping("/page")
    public Result<Page<SeckillActivity>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "状态过滤") @RequestParam(required = false) Integer status) {
        return Result.ok(seckillService.pageActivities(current, size, status));
    }

    @Operation(summary = "活动详情（含商品）")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<SeckillActivityVO> detail(@Parameter(description = "活动 ID") @PathVariable Long id) {
        return Result.ok(seckillService.getActivityDetail(id));
    }

    @Operation(summary = "创建活动")
    @SaCheckRole("super_admin")
    @PostMapping
    public Result<Long> create(@Valid @RequestBody SeckillActivitySaveReq req) {
        return Result.ok(seckillService.createActivity(req));
    }

    @Operation(summary = "更新活动")
    @SaCheckRole("super_admin")
    @PutMapping
    public Result<Void> update(@Valid @RequestBody SeckillActivitySaveReq req) {
        seckillService.updateActivity(req);
        return Result.ok();
    }

    @Operation(summary = "删除活动（级联清理 Redis）")
    @SaCheckRole("super_admin")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "活动 ID") @PathVariable Long id) {
        seckillService.deleteActivity(id);
        return Result.ok();
    }

    @Operation(summary = "手动结束活动")
    @SaCheckRole("super_admin")
    @PutMapping("/{id}/end")
    public Result<Void> forceEnd(@Parameter(description = "活动 ID") @PathVariable Long id) {
        seckillService.forceEndActivity(id);
        return Result.ok();
    }

    @Operation(summary = "预热活动（同步库存到 Redis）")
    @SaCheckRole("super_admin")
    @PostMapping("/{id}/warmup")
    public Result<Void> warmup(@Parameter(description = "活动 ID") @PathVariable Long id) {
        seckillService.warmupActivity(id);
        return Result.ok();
    }

    @Operation(summary = "给活动添加商品")
    @SaCheckRole("super_admin")
    @PostMapping("/product")
    public Result<Long> addProduct(@Valid @RequestBody SeckillProductAddReq req) {
        return Result.ok(seckillService.addProduct(req));
    }

    @Operation(summary = "移除活动商品")
    @SaCheckRole("super_admin")
    @DeleteMapping("/product/{id}")
    public Result<Void> removeProduct(@Parameter(description = "活动商品 ID") @PathVariable Long id) {
        seckillService.removeProduct(id);
        return Result.ok();
    }
}
