package com.ycwl.basic.pricing.controller;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.pricing.dto.PricingFilterRequest;
import com.ycwl.basic.pricing.dto.TierConfigFilterRequest;
import com.ycwl.basic.pricing.dto.BundleConfigFilterRequest;
import com.ycwl.basic.pricing.entity.PriceProductConfig;
import com.ycwl.basic.pricing.entity.PriceTierConfig;
import com.ycwl.basic.pricing.entity.PriceBundleConfig;
import com.ycwl.basic.pricing.entity.PriceCouponConfig;
import com.ycwl.basic.pricing.entity.PriceCouponClaimRecord;
import com.ycwl.basic.pricing.service.IProductConfigService;
import com.ycwl.basic.pricing.service.IPriceBundleService;
import com.ycwl.basic.pricing.service.IPricingManagementService;
import com.ycwl.basic.pricing.service.ICouponManagementService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 价格配置管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/pricing/config")
@RequiredArgsConstructor
public class PricingConfigController {
    
    private final IProductConfigService productConfigService;
    private final IPriceBundleService bundleService;
    private final IPricingManagementService managementService;
    private final ICouponManagementService couponManagementService;
    
    
    // ==================== 查询API ====================
    
    /**
     * 获取所有商品配置
     */
    @GetMapping("/products")
    public ApiResponse<List<PriceProductConfig>> getAllProductConfigs() {
        log.info("获取所有商品配置");
        List<PriceProductConfig> configs = productConfigService.getAllProductConfigs();
        return ApiResponse.success(configs);
    }
    
    /**
     * 根据商品类型获取阶梯配置
     */
    @GetMapping("/tiers/{productType}")
    public ApiResponse<List<PriceTierConfig>> getTierConfigs(@PathVariable String productType) {
        log.info("根据商品类型获取阶梯配置: {}", productType);
        List<PriceTierConfig> configs = productConfigService.getTierConfigs(productType);
        return ApiResponse.success(configs);
    }
    
    /**
     * 根据商品类型和商品ID获取阶梯配置
     */
    @GetMapping("/tiers/{productType}/{productId}")
    public ApiResponse<List<PriceTierConfig>> getTierConfigs(@PathVariable String productType, 
                                                           @PathVariable String productId) {
        log.info("根据商品类型和ID获取阶梯配置: {}, {}", productType, productId);
        List<PriceTierConfig> configs = productConfigService.getTierConfigs(productType, productId);
        return ApiResponse.success(configs);
    }
    
    /**
     * 根据商品类型和商品ID获取具体配置
     */
    @GetMapping("/products/{productType}/{productId}")
    public ApiResponse<PriceProductConfig> getProductConfig(@PathVariable String productType, 
                                                          @PathVariable String productId) {
        log.info("根据商品类型和ID获取商品配置: {}, {}", productType, productId);
        PriceProductConfig config = productConfigService.getProductConfig(productType, productId);
        return ApiResponse.success(config);
    }
    
    /**
     * 获取所有阶梯配置
     */
    @GetMapping("/tiers")
    public ApiResponse<List<PriceTierConfig>> getAllTierConfigs() {
        log.info("获取所有阶梯配置");
        List<PriceTierConfig> configs = productConfigService.getAllTierConfigs();
        return ApiResponse.success(configs);
    }
    
    /**
     * 获取所有一口价配置
     */
    @GetMapping("/bundles")
    public ApiResponse<List<PriceBundleConfig>> getAllBundleConfigs() {
        log.info("获取所有一口价配置");
        List<PriceBundleConfig> configs = bundleService.getAllBundles();
        return ApiResponse.success(configs);
    }
    
    
    // ==================== 配置管理API（手动处理时间） ====================
    
    /**
     * 创建商品配置
     */
    @PostMapping("/products")
    public ApiResponse<Long> createProductConfig(@RequestBody PriceProductConfig config) {
        log.info("创建商品配置: {}", config.getProductName());
        Long id = managementService.createProductConfig(config);
        return ApiResponse.success(id);
    }
    
    /**
     * 更新商品配置
     */
    @PutMapping("/products/{id}")
    public ApiResponse<Boolean> updateProductConfig(@PathVariable Long id, @RequestBody PriceProductConfig config) {
        log.info("更新商品配置: id={}, name={}", id, config.getProductName());
        config.setId(id);
        boolean success = managementService.updateProductConfig(config);
        return ApiResponse.success(success);
    }
    
    /**
     * 创建阶梯定价配置
     */
    @PostMapping("/tiers")
    public ApiResponse<Long> createTierConfig(@RequestBody PriceTierConfig config) {
        log.info("创建阶梯定价配置: productType={}, price={}", config.getProductType(), config.getPrice());
        Long id = managementService.createTierConfig(config);
        return ApiResponse.success(id);
    }
    
    /**
     * 更新阶梯定价配置
     */
    @PutMapping("/tiers/{id}")
    public ApiResponse<Boolean> updateTierConfig(@PathVariable Long id, @RequestBody PriceTierConfig config) {
        log.info("更新阶梯定价配置: id={}", id);
        config.setId(id);
        boolean success = managementService.updateTierConfig(config);
        return ApiResponse.success(success);
    }
    
    /**
     * 创建一口价配置
     */
    @PostMapping("/bundles")
    public ApiResponse<Long> createBundleConfig(@RequestBody PriceBundleConfig config) {
        log.info("创建一口价配置: {}", config.getBundleName());
        Long id = managementService.createBundleConfig(config);
        return ApiResponse.success(id);
    }
    
    /**
     * 更新一口价配置
     */
    @PutMapping("/bundles/{id}")
    public ApiResponse<Boolean> updateBundleConfig(@PathVariable Long id, @RequestBody PriceBundleConfig config) {
        log.info("更新一口价配置: id={}, name={}", id, config.getBundleName());
        config.setId(id);
        boolean success = managementService.updateBundleConfig(config);
        return ApiResponse.success(success);
    }
    
    // ==================== 启用/禁用API ====================
    
    /**
     * 启用/禁用商品配置
     */
    @PutMapping("/products/{id}/status")
    public ApiResponse<Boolean> updateProductConfigStatus(@PathVariable Long id, @RequestParam Boolean isActive) {
        log.info("修改商品配置状态: id={}, isActive={}", id, isActive);
        boolean success = managementService.updateProductConfigStatus(id, isActive);
        return ApiResponse.success(success);
    }
    
    /**
     * 启用/禁用阶梯配置
     */
    @PutMapping("/tiers/{id}/status")
    public ApiResponse<Boolean> updateTierConfigStatus(@PathVariable Long id, @RequestParam Boolean isActive) {
        log.info("修改阶梯配置状态: id={}, isActive={}", id, isActive);
        boolean success = managementService.updateTierConfigStatus(id, isActive);
        return ApiResponse.success(success);
    }
    
    /**
     * 启用/禁用一口价配置
     */
    @PutMapping("/bundles/{id}/status")
    public ApiResponse<Boolean> updateBundleConfigStatus(@PathVariable Long id, @RequestParam Boolean isActive) {
        log.info("修改一口价配置状态: id={}, isActive={}", id, isActive);
        boolean success = managementService.updateBundleConfigStatus(id, isActive);
        return ApiResponse.success(success);
    }
    
    // ==================== 删除API ====================
    
    /**
     * 删除商品配置
     */
    @DeleteMapping("/products/{id}")
    public ApiResponse<Boolean> deleteProductConfig(@PathVariable Long id) {
        log.info("删除商品配置: id={}", id);
        boolean success = managementService.deleteProductConfig(id);
        return ApiResponse.success(success);
    }
    
    /**
     * 删除阶梯配置
     */
    @DeleteMapping("/tiers/{id}")
    public ApiResponse<Boolean> deleteTierConfig(@PathVariable Long id) {
        log.info("删除阶梯配置: id={}", id);
        boolean success = managementService.deleteTierConfig(id);
        return ApiResponse.success(success);
    }
    
    /**
     * 删除一口价配置
     */
    @DeleteMapping("/bundles/{id}")
    public ApiResponse<Boolean> deleteBundleConfig(@PathVariable Long id) {
        log.info("删除一口价配置: id={}", id);
        boolean success = managementService.deleteBundleConfig(id);
        return ApiResponse.success(success);
    }
    
    // ==================== 管理端接口（包含禁用的配置） ====================
    
    /**
     * 管理端：获取所有商品配置（包含禁用的）
     */
    @GetMapping("/admin/products")
    public ApiResponse<List<PriceProductConfig>> getAllProductConfigsForAdmin() {
        log.info("管理端获取所有商品配置");
        List<PriceProductConfig> configs = productConfigService.getAllProductConfigsForAdmin();
        return ApiResponse.success(configs);
    }
    
    /**
     * 管理端：分页查询商品配置（包含禁用的），支持筛选
     */
    @PostMapping("/admin/products/page")
    public ApiResponse<PageInfo<PriceProductConfig>> pageProductConfigsForAdmin(@RequestBody PricingFilterRequest request) {
        log.info("管理端分页查询商品配置: {}", request);
        PageInfo<PriceProductConfig> pageInfo = productConfigService.pageProductConfigsForAdmin(request);
        return ApiResponse.success(pageInfo);
    }
    
    /**
     * 管理端：获取所有阶梯配置（包含禁用的）
     */
    @GetMapping("/admin/tiers")
    public ApiResponse<List<PriceTierConfig>> getAllTierConfigsForAdmin() {
        log.info("管理端获取所有阶梯配置");
        List<PriceTierConfig> configs = productConfigService.getAllTierConfigsForAdmin();
        return ApiResponse.success(configs);
    }
    
    /**
     * 管理端：分页查询阶梯配置（包含禁用的），支持筛选
     */
    @PostMapping("/admin/tiers/page")
    public ApiResponse<PageInfo<PriceTierConfig>> pageTierConfigsForAdmin(@RequestBody TierConfigFilterRequest request) {
        log.info("管理端分页查询阶梯配置: {}", request);
        PageInfo<PriceTierConfig> pageInfo = productConfigService.pageTierConfigsForAdmin(request);
        return ApiResponse.success(pageInfo);
    }
    
    /**
     * 管理端：根据商品类型获取阶梯配置（包含禁用的）
     */
    @GetMapping("/admin/tiers/{productType}")
    public ApiResponse<List<PriceTierConfig>> getTierConfigsForAdmin(@PathVariable String productType) {
        log.info("管理端根据商品类型获取阶梯配置: {}", productType);
        List<PriceTierConfig> configs = productConfigService.getTierConfigsForAdmin(productType);
        return ApiResponse.success(configs);
    }
    
    /**
     * 管理端：根据商品类型和商品ID获取阶梯配置（包含禁用的）
     */
    @GetMapping("/admin/tiers/{productType}/{productId}")
    public ApiResponse<List<PriceTierConfig>> getTierConfigsForAdmin(@PathVariable String productType, 
                                                                   @PathVariable String productId) {
        log.info("管理端根据商品类型和ID获取阶梯配置: {}, {}", productType, productId);
        List<PriceTierConfig> configs = productConfigService.getTierConfigsForAdmin(productType, productId);
        return ApiResponse.success(configs);
    }
    
    /**
     * 管理端：获取所有一口价配置（包含禁用的）
     */
    @GetMapping("/admin/bundles")
    public ApiResponse<List<PriceBundleConfig>> getAllBundleConfigsForAdmin() {
        log.info("管理端获取所有一口价配置");
        List<PriceBundleConfig> configs = bundleService.getAllBundlesForAdmin();
        return ApiResponse.success(configs);
    }
    
    /**
     * 管理端：分页查询一口价配置（包含禁用的），支持筛选
     */
    @PostMapping("/admin/bundles/page")
    public ApiResponse<PageInfo<PriceBundleConfig>> pageBundleConfigsForAdmin(@RequestBody BundleConfigFilterRequest request) {
        log.info("管理端分页查询一口价配置: {}", request);
        PageInfo<PriceBundleConfig> pageInfo = bundleService.pageBundleConfigsForAdmin(request);
        return ApiResponse.success(pageInfo);
    }
    
    /**
     * 管理端：获取所有优惠券配置（包含禁用的）
     */
    @GetMapping("/admin/coupons")
    public ApiResponse<List<PriceCouponConfig>> getAllCouponConfigsForAdmin() {
        log.info("管理端获取所有优惠券配置");
        List<PriceCouponConfig> configs = couponManagementService.getAllCouponConfigs();
        return ApiResponse.success(configs);
    }
    
    /**
     * 管理端：获取所有优惠券领取记录
     */
    @GetMapping("/admin/coupon-records")
    public ApiResponse<List<PriceCouponClaimRecord>> getAllCouponClaimRecordsForAdmin() {
        log.info("管理端获取所有优惠券领取记录");
        List<PriceCouponClaimRecord> records = couponManagementService.getAllClaimRecords();
        return ApiResponse.success(records);
    }
}