package org.dromara.mall.coupon.controller.app;

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.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.mall.coupon.domain.bo.MallCouponUserBo;
import org.dromara.mall.coupon.domain.vo.MallCouponUserVo;
import org.dromara.mall.coupon.service.MallCouponUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户优惠券（移动端）
 *
 * @author pengles
 */
@Tag(name = "移动端-用户优惠券", description = "用户优惠券接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/mall/coupon/user")
public class AppMallCouponUserController {

    private final MallCouponUserService mallCouponUserService;

    /**
     * 查询用户优惠券列表
     */
    @Operation(summary = "查询用户优惠券列表", description = "查询当前用户的优惠券列表")
    @GetMapping("/list")
    public R<TableDataInfo<MallCouponUserVo>> list(@Parameter(description = "用户优惠券查询条件") MallCouponUserBo bo,
                                                   @Parameter(description = "分页参数") PageQuery pageQuery) {
        // 移动端只能查询当前用户的优惠券
        TableDataInfo<MallCouponUserVo> result = mallCouponUserService.queryPageList(bo, pageQuery);
        return R.ok(result);
    }

    /**
     * 查询用户可用优惠券列表
     */
    @Operation(summary = "查询用户可用优惠券列表", description = "查询当前用户可用的优惠券列表（未使用且未过期）")
    @GetMapping("/available/list")
    public R<List<MallCouponUserVo>> getAvailableList() {
        Long userId = org.dromara.common.satoken.utils.LoginHelper.getUserId();
        List<MallCouponUserVo> list = mallCouponUserService.queryAvailableList(userId);
        return R.ok(list);
    }

    /**
     * 获取用户优惠券详情
     */
    @Operation(summary = "获取用户优惠券详情", description = "根据用户优惠券ID获取详细信息")
    @GetMapping("/{id}")
    public R<MallCouponUserVo> getInfo(@Parameter(description = "用户优惠券ID", required = true)
                                       @NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(mallCouponUserService.queryById(id));
    }

    /**
     * 领取优惠券
     */
    @Operation(summary = "领取优惠券", description = "用户领取优惠券")
    @PostMapping("/receive/{couponId}")
    public R<Void> receiveCoupon(@Parameter(description = "优惠券ID", required = true)
                                 @NotNull(message = "优惠券ID不能为空")
                                 @PathVariable Long couponId) {
        return R.ok(mallCouponUserService.receiveCoupon(couponId));
    }

}
