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

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.snack.server.common.Result;
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.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券（用户端）
 */
@Tag(name = "优惠券（用户端）")
@RestController
@RequiredArgsConstructor
public class CouponUserController {

    private final CouponService couponService;

    @Operation(summary = "可领取优惠券列表")
    @GetMapping("/api/coupons")
    public Result<List<CouponVO>> listReceivable() {
        Long userId = StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null;
        return Result.ok(couponService.listReceivableCoupons(userId));
    }

    @Operation(summary = "领取优惠券")
    @SaCheckLogin
    @PostMapping("/api/coupons/{id}/receive")
    public Result<Long> receive(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        return Result.ok(couponService.receiveCoupon(id, userId));
    }

    @Operation(summary = "我的优惠券")
    @SaCheckLogin
    @GetMapping("/api/user/coupons")
    public Result<List<UserCouponVO>> myCoupons(@RequestParam(required = false) Integer status) {
        Long userId = StpUtil.getLoginIdAsLong();
        return Result.ok(couponService.listMyCoupons(userId, status));
    }
}
