package com.ycwl.basic.pricing.controller;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.constant.BaseContextHandler;
import com.ycwl.basic.pricing.dto.req.VoucherBatchCreateReq;
import com.ycwl.basic.pricing.dto.req.VoucherBatchCreateReqV2;
import com.ycwl.basic.pricing.dto.req.VoucherBatchQueryReq;
import com.ycwl.basic.pricing.dto.req.VoucherClaimReq;
import com.ycwl.basic.pricing.dto.req.VoucherCodeQueryReq;
import com.ycwl.basic.pricing.dto.resp.VoucherBatchResp;
import com.ycwl.basic.pricing.dto.resp.VoucherBatchStatsResp;
import com.ycwl.basic.pricing.dto.resp.VoucherCodeResp;
import com.ycwl.basic.pricing.service.VoucherBatchService;
import com.ycwl.basic.pricing.service.VoucherCodeService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/pricing/voucher")
public class VoucherManagementController {
    @Autowired
    @Lazy
    private VoucherBatchService voucherBatchService;
    @Autowired
    @Lazy
    private VoucherCodeService voucherCodeService;
    
    @PostMapping("/batch/create")
    public ApiResponse<Long> createBatch(@RequestBody VoucherBatchCreateReq req) {
        Long batchId = voucherBatchService.createBatch(req);
        return ApiResponse.success(batchId);
    }
    
    @PostMapping("/batch/create/v2")
    public ApiResponse<Long> createBatchV2(@RequestBody VoucherBatchCreateReqV2 req) {
        Long batchId = voucherBatchService.createBatchV2(req);
        return ApiResponse.success(batchId);
    }
    
    @PostMapping("/batch/list")
    public ApiResponse<PageInfo<VoucherBatchResp>> getBatchList(@RequestBody VoucherBatchQueryReq req) {
        PageInfo<VoucherBatchResp> page = voucherBatchService.queryBatchList(req);
        return ApiResponse.success(page);
    }
    
    @GetMapping("/batch/{id}")
    public ApiResponse<VoucherBatchResp> getBatchDetail(@PathVariable Long id) {
        VoucherBatchResp batch = voucherBatchService.getBatchDetail(id);
        return ApiResponse.success(batch);
    }
    
    @GetMapping("/batch/{id}/stats")
    public ApiResponse<VoucherBatchStatsResp> getBatchStats(@PathVariable Long id) {
        VoucherBatchStatsResp stats = voucherBatchService.getBatchStats(id);
        return ApiResponse.success(stats);
    }
    
    @PutMapping("/batch/{id}/status")
    public ApiResponse<Void> updateBatchStatus(@PathVariable Long id, @RequestParam Integer status) {
        voucherBatchService.updateBatchStatus(id, status);
        return ApiResponse.success(null);
    }
    
    @PostMapping("/codes")
    public ApiResponse<PageInfo<VoucherCodeResp>> getCodeList(@RequestBody VoucherCodeQueryReq req) {
        PageInfo<VoucherCodeResp> page = voucherCodeService.queryCodeList(req);
        return ApiResponse.success(page);
    }
    
    @PutMapping("/code/{id}/use")
    public ApiResponse<Void> markCodeAsUsed(@PathVariable Long id, @RequestParam(required = false) String remark) {
        voucherCodeService.markCodeAsUsed(id, remark);
        return ApiResponse.success(null);
    }
    
    @GetMapping("/scenic/{scenicId}/users")
    public ApiResponse<PageInfo<VoucherCodeResp>> getUsersInScenic(@PathVariable Long scenicId,
                                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                                              @RequestParam(defaultValue = "10") Integer pageSize) {
        VoucherCodeQueryReq req = new VoucherCodeQueryReq();
        req.setScenicId(scenicId);
        req.setPageNum(pageNum);
        req.setPageSize(pageSize);
        PageInfo<VoucherCodeResp> page = voucherCodeService.queryCodeList(req);
        return ApiResponse.success(page);
    }
    
    @PostMapping("/mobile/claim")
    public ApiResponse<VoucherCodeResp> claimVoucher(@RequestBody VoucherClaimReq req) {
        VoucherCodeResp result = voucherCodeService.claimVoucher(req);
        return ApiResponse.success(result);
    }
    
    @GetMapping("/mobile/my-codes")
    public ApiResponse<List<VoucherCodeResp>> getMyVoucherCodes() {
        Long userId = Long.valueOf(BaseContextHandler.getUserId());
        List<VoucherCodeResp> codes = voucherCodeService.getMyVoucherCodes(userId);
        return ApiResponse.success(codes);
    }
}