package com.ycwl.basic.pricing.controller;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.pricing.dto.req.VoucherUsageHistoryReq;
import com.ycwl.basic.pricing.dto.resp.VoucherUsageRecordResp;
import com.ycwl.basic.pricing.dto.resp.VoucherUsageStatsResp;
import com.ycwl.basic.pricing.service.IVoucherUsageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 券码使用记录管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/pricing/voucher/usage")
@RequiredArgsConstructor
public class VoucherUsageController {

    private final IVoucherUsageService voucherUsageService;

    @PostMapping("/history")
    public ApiResponse<PageInfo<VoucherUsageRecordResp>> getUsageHistory(@RequestBody VoucherUsageHistoryReq req) {
        try {
            PageInfo<VoucherUsageRecordResp> result = voucherUsageService.getUsageHistory(req);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("查询券码使用记录失败", e);
            return ApiResponse.fail("查询失败: " + e.getMessage());
        }
    }

    @GetMapping("/{voucherCode}/records")
    public ApiResponse<List<VoucherUsageRecordResp>> getRecordsByCode(
            @PathVariable String voucherCode) {
        try {
            List<VoucherUsageRecordResp> records = voucherUsageService.getUsageRecordsByCode(voucherCode);
            return ApiResponse.success(records);
        } catch (Exception e) {
            log.error("查询券码使用记录失败: {}", voucherCode, e);
            return ApiResponse.fail("查询失败: " + e.getMessage());
        }
    }

    @GetMapping("/{voucherCode}/stats")
    public ApiResponse<VoucherUsageStatsResp> getUsageStats(
            @PathVariable String voucherCode) {
        try {
            VoucherUsageStatsResp stats = voucherUsageService.getUsageStats(voucherCode);
            if (stats == null) {
                return ApiResponse.fail("券码不存在");
            }
            return ApiResponse.success(stats);
        } catch (Exception e) {
            log.error("查询券码统计信息失败: {}", voucherCode, e);
            return ApiResponse.fail("查询失败: " + e.getMessage());
        }
    }

    @GetMapping("/user/{faceId}/scenic/{scenicId}")
    public ApiResponse<List<VoucherUsageRecordResp>> getUserUsageRecords(
            @PathVariable Long faceId,
            @PathVariable Long scenicId) {
        try {
            List<VoucherUsageRecordResp> records = voucherUsageService.getUserUsageRecords(faceId, scenicId);
            return ApiResponse.success(records);
        } catch (Exception e) {
            log.error("查询用户券码使用记录失败: faceId={}, scenicId={}", faceId, scenicId, e);
            return ApiResponse.fail("查询失败: " + e.getMessage());
        }
    }

    @GetMapping("/batch/{batchId}/stats")
    public ApiResponse<List<VoucherUsageStatsResp>> getBatchUsageStats(
            @PathVariable Long batchId) {
        try {
            List<VoucherUsageStatsResp> statsList = voucherUsageService.getBatchUsageStats(batchId);
            return ApiResponse.success(statsList);
        } catch (Exception e) {
            log.error("查询批次券码统计信息失败: batchId={}", batchId, e);
            return ApiResponse.fail("查询失败: " + e.getMessage());
        }
    }
}