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

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.snack.server.common.Result;
import com.snack.server.module.seckill.dto.req.SeckillBuyReq;
import com.snack.server.module.seckill.service.SeckillService;
import com.snack.server.module.seckill.vo.SeckillActivityVO;
import com.snack.server.module.seckill.vo.SeckillResultVO;
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.*;

import java.util.List;
import java.util.Map;

/**
 * 抢购（用户端）
 */
@Tag(name = "限时抢购")
@RestController
@RequestMapping("/api/seckill")
@RequiredArgsConstructor
public class SeckillController {

    private final SeckillService seckillService;

    @Operation(summary = "获取进行中 / 即将开始的抢购活动")
    @GetMapping("/activities")
    public Result<List<SeckillActivityVO>> activities() {
        return Result.ok(seckillService.listActiveActivities());
    }

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

    @Operation(summary = "抢购下单（核心接口）")
    @SaCheckLogin
    @PostMapping("/buy")
    public Result<Map<String, String>> buy(@Valid @RequestBody SeckillBuyReq req) {
        String orderNo = seckillService.seckillBuy(req);
        return Result.ok(Map.of("orderNo", orderNo));
    }

    @Operation(summary = "查询抢购结果（轮询接口）")
    @SaCheckLogin
    @GetMapping("/result/{orderNo}")
    public Result<SeckillResultVO> result(@Parameter(description = "排队号 orderNo") @PathVariable String orderNo) {
        return Result.ok(seckillService.queryResult(orderNo));
    }
}
