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

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.server.common.Result;
import com.snack.server.module.coupon.dto.req.CouponPageReq;
import com.snack.server.module.coupon.dto.req.CouponSaveReq;
import com.snack.server.module.coupon.dto.req.UserCouponPageReq;
import com.snack.server.module.coupon.service.CouponService;
import com.snack.server.module.coupon.vo.CouponVO;
import com.snack.server.module.coupon.vo.UserCouponVO;
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.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券管理（管理端）
 */
@Tag(name = "优惠券管理")
@RestController
@RequestMapping("/api/admin/coupon")
@RequiredArgsConstructor
public class CouponAdminController {

    private final CouponService couponService;

    @Operation(summary = "分页查询优惠券模板")
    @SaCheckLogin
    @GetMapping("/page")
    public Result<Page<CouponVO>> page(CouponPageReq req) {
        return Result.ok(couponService.pageCoupons(req));
    }

    @Operation(summary = "优惠券模板详情")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<CouponVO> detail(@Parameter(description = "优惠券 ID") @PathVariable Long id) {
        return Result.ok(couponService.getCouponById(id));
    }

    @Operation(summary = "创建优惠券模板")
    @SaCheckLogin
    @PostMapping
    public Result<Long> create(@Valid @RequestBody CouponSaveReq req) {
        return Result.ok(couponService.createCoupon(req));
    }

    @Operation(summary = "更新优惠券模板")
    @SaCheckLogin
    @PutMapping
    public Result<Void> update(@Valid @RequestBody CouponSaveReq req) {
        couponService.updateCoupon(req);
        return Result.ok();
    }

    @Operation(summary = "删除优惠券模板")
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "优惠券 ID") @PathVariable Long id) {
        couponService.deleteCoupon(id);
        return Result.ok();
    }

    @Operation(summary = "更新优惠券状态")
    @SaCheckLogin
    @PutMapping("/{id}/status/{status}")
    public Result<Void> updateStatus(
            @Parameter(description = "优惠券 ID") @PathVariable Long id,
            @Parameter(description = "状态：0-未上线 1-已上线 2-已结束") @PathVariable Integer status) {
        couponService.updateStatus(id, status);
        return Result.ok();
    }

    @Operation(summary = "更新优惠券状态（兼容旧前端）")
    @SaCheckLogin
    @PutMapping("/{id}/status")
    public Result<Void> updateStatusCompat(
            @Parameter(description = "优惠券 ID") @PathVariable Long id,
            @RequestBody StatusReq req) {
        couponService.updateStatus(id, req.getStatus());
        return Result.ok();
    }

    @Operation(summary = "分页查询领取记录")
    @SaCheckLogin
    @GetMapping("/record/page")
    public Result<Page<UserCouponVO>> recordPage(UserCouponPageReq req) {
        return Result.ok(couponService.pageUserCoupons(req));
    }

    @Operation(summary = "查询某优惠券的领取记录")
    @SaCheckLogin
    @GetMapping("/{id}/records")
    public Result<Page<UserCouponVO>> recordsOfCoupon(
            @Parameter(description = "优惠券 ID") @PathVariable Long id,
            UserCouponPageReq req) {
        req.setCouponId(id);
        return Result.ok(couponService.pageUserCoupons(req));
    }

    @Data
    public static class StatusReq {
        private Integer status;
    }
}
