package com.ycwl.basic.pricing.controller;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.pricing.entity.PriceCouponClaimRecord;
import com.ycwl.basic.pricing.entity.PriceCouponConfig;
import com.ycwl.basic.pricing.enums.CouponStatus;
import com.ycwl.basic.pricing.service.ICouponManagementService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 优惠券管理控制器（管理端）
 */
@Slf4j
@RestController
@RequestMapping("/api/pricing/admin/coupons")
@RequiredArgsConstructor
public class CouponManagementController {
    
    private final ICouponManagementService couponManagementService;
    
    // ==================== 优惠券配置管理 ====================
    
    /**
     * 创建优惠券配置
     */
    @PostMapping("/configs")
    public ApiResponse<Long> createCouponConfig(@RequestBody PriceCouponConfig config) {
        log.info("创建优惠券配置: {}", config.getCouponName());
        Long id = couponManagementService.createCouponConfig(config);
        return ApiResponse.success(id);
    }
    
    /**
     * 更新优惠券配置
     */
    @PutMapping("/configs/{id}")
    public ApiResponse<Boolean> updateCouponConfig(@PathVariable Long id, @RequestBody PriceCouponConfig config) {
        log.info("更新优惠券配置: id={}, name={}", id, config.getCouponName());
        config.setId(id);
        boolean success = couponManagementService.updateCouponConfig(config);
        return ApiResponse.success(success);
    }
    
    /**
     * 删除优惠券配置
     */
    @DeleteMapping("/configs/{id}")
    public ApiResponse<Boolean> deleteCouponConfig(@PathVariable Long id) {
        log.info("删除优惠券配置: id={}", id);
        boolean success = couponManagementService.deleteCouponConfig(id);
        return ApiResponse.success(success);
    }
    
    /**
     * 启用/禁用优惠券配置
     */
    @PutMapping("/configs/{id}/status")
    public ApiResponse<Boolean> updateCouponConfigStatus(@PathVariable Long id, @RequestParam Boolean isActive) {
        log.info("修改优惠券配置状态: id={}, isActive={}", id, isActive);
        boolean success = couponManagementService.updateCouponConfigStatus(id, isActive);
        return ApiResponse.success(success);
    }
    
    /**
     * 查询所有优惠券配置（包含禁用的）
     */
    @GetMapping("/configs")
    public ApiResponse<List<PriceCouponConfig>> getAllCouponConfigs() {
        log.info("管理端获取所有优惠券配置");
        List<PriceCouponConfig> configs = couponManagementService.getAllCouponConfigs();
        return ApiResponse.success(configs);
    }
    
    /**
     * 分页查询优惠券配置
     */
    @GetMapping("/configs/page")
    public ApiResponse<PageInfo<PriceCouponConfig>> getCouponConfigsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Boolean isActive,
            @RequestParam(required = false) String couponName,
            @RequestParam(required = false) String scenicId) {
        log.info("分页查询优惠券配置: pageNum={}, pageSize={}, isActive={}, couponName={}, scenicId={}", 
                pageNum, pageSize, isActive, couponName, scenicId);
        PageInfo<PriceCouponConfig> pageInfo = couponManagementService.getCouponConfigsPage(
                pageNum, pageSize, isActive, couponName, scenicId);
        return ApiResponse.success(pageInfo);
    }
    
    /**
     * 根据状态查询优惠券配置
     */
    @GetMapping("/configs/status/{isActive}")
    public ApiResponse<List<PriceCouponConfig>> getCouponConfigsByStatus(@PathVariable Boolean isActive) {
        log.info("根据状态查询优惠券配置: {}", isActive);
        List<PriceCouponConfig> configs = couponManagementService.getCouponConfigsByStatus(isActive);
        return ApiResponse.success(configs);
    }
    
    /**
     * 根据ID查询优惠券配置
     */
    @GetMapping("/configs/{id}")
    public ApiResponse<PriceCouponConfig> getCouponConfigById(@PathVariable Long id) {
        log.info("根据ID查询优惠券配置: {}", id);
        PriceCouponConfig config = couponManagementService.getCouponConfigById(id);
        return ApiResponse.success(config);
    }
    
    // ==================== 优惠券领取记录查询 ====================
    
    /**
     * 查询所有优惠券领取记录
     */
    @GetMapping("/claim-records")
    public ApiResponse<List<PriceCouponClaimRecord>> getAllClaimRecords() {
        log.info("查询所有优惠券领取记录");
        List<PriceCouponClaimRecord> records = couponManagementService.getAllClaimRecords();
        return ApiResponse.success(records);
    }
    
    /**
     * 分页查询优惠券领取记录
     */
    @GetMapping("/claim-records/page")
    public ApiResponse<PageInfo<PriceCouponClaimRecord>> getClaimRecordsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) Long couponId,
            @RequestParam(required = false) CouponStatus status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String scenicId) {
        log.info("分页查询优惠券领取记录: pageNum={}, pageSize={}, userId={}, couponId={}, status={}, startTime={}, endTime={}, scenicId={}", 
                pageNum, pageSize, userId, couponId, status, startTime, endTime, scenicId);
        PageInfo<PriceCouponClaimRecord> pageInfo = couponManagementService.getClaimRecordsPage(
                pageNum, pageSize, userId, couponId, status, startTime, endTime, scenicId);
        return ApiResponse.success(pageInfo);
    }
    
    /**
     * 根据用户ID查询优惠券领取记录
     */
    @GetMapping("/claim-records/user/{userId}")
    public ApiResponse<List<PriceCouponClaimRecord>> getClaimRecordsByUserId(@PathVariable Long userId) {
        log.info("根据用户ID查询优惠券领取记录: {}", userId);
        List<PriceCouponClaimRecord> records = couponManagementService.getClaimRecordsByUserId(userId);
        return ApiResponse.success(records);
    }
    
    /**
     * 根据优惠券ID查询领取记录
     */
    @GetMapping("/claim-records/coupon/{couponId}")
    public ApiResponse<List<PriceCouponClaimRecord>> getClaimRecordsByCouponId(@PathVariable Long couponId) {
        log.info("根据优惠券ID查询领取记录: {}", couponId);
        List<PriceCouponClaimRecord> records = couponManagementService.getClaimRecordsByCouponId(couponId);
        return ApiResponse.success(records);
    }
    
    /**
     * 根据状态查询领取记录
     */
    @GetMapping("/claim-records/status/{status}")
    public ApiResponse<List<PriceCouponClaimRecord>> getClaimRecordsByStatus(@PathVariable CouponStatus status) {
        log.info("根据状态查询领取记录: {}", status);
        List<PriceCouponClaimRecord> records = couponManagementService.getClaimRecordsByStatus(status);
        return ApiResponse.success(records);
    }
    
    // ==================== 统计功能 ====================
    
    /**
     * 查询优惠券使用统计
     */
    @GetMapping("/stats/{couponId}")
    public ApiResponse<Map<String, Object>> getCouponUsageStats(@PathVariable Long couponId) {
        log.info("查询优惠券使用统计: {}", couponId);
        Map<String, Object> stats = couponManagementService.getCouponUsageStats(couponId);
        return ApiResponse.success(stats);
    }
    
    /**
     * 查询优惠券详细统计
     */
    @GetMapping("/stats/{couponId}/detail")
    public ApiResponse<Map<String, Object>> getCouponDetailStats(@PathVariable Long couponId) {
        log.info("查询优惠券详细统计: {}", couponId);
        Map<String, Object> stats = couponManagementService.getCouponDetailStats(couponId);
        return ApiResponse.success(stats);
    }
    
    /**
     * 查询时间范围内的统计数据
     */
    @GetMapping("/stats/period")
    public ApiResponse<Map<String, Object>> getPeriodStats(
            @RequestParam String startDate,
            @RequestParam String endDate,
            @RequestParam(required = false) String scenicId) {
        log.info("查询时间范围统计: startDate={}, endDate={}, scenicId={}", startDate, endDate, scenicId);
        Map<String, Object> stats = couponManagementService.getPeriodStats(startDate, endDate, scenicId);
        return ApiResponse.success(stats);
    }
    
    /**
     * 查询景区优惠券统计
     */
    @GetMapping("/stats/scenic/{scenicId}")
    public ApiResponse<Map<String, Object>> getScenicCouponStats(@PathVariable String scenicId) {
        log.info("查询景区优惠券统计: scenicId={}", scenicId);
        Map<String, Object> stats = couponManagementService.getScenicCouponStats(scenicId);
        return ApiResponse.success(stats);
    }
    
    /**
     * 查询所有优惠券的使用统计概览
     */
    @GetMapping("/stats/overview")
    public ApiResponse<List<Map<String, Object>>> getAllCouponUsageOverview() {
        log.info("查询所有优惠券使用统计概览");
        List<Map<String, Object>> overview = couponManagementService.getAllCouponUsageOverview();
        return ApiResponse.success(overview);
    }
}