package com.ycwl.basic.pricing.controller;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.pricing.dto.OnePriceConfigFilterRequest;
import com.ycwl.basic.pricing.dto.OnePriceConfigRequest;
import com.ycwl.basic.pricing.entity.PriceOnePriceConfig;
import com.ycwl.basic.pricing.service.IOnePricePurchaseService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一口价购买管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/pricing/admin/one-price")
@RequiredArgsConstructor
public class OnePricePurchaseController {
    
    private final IOnePricePurchaseService onePricePurchaseService;
    
    /**
     * 分页查询一口价配置
     */
    @GetMapping("/")
    public ApiResponse<PageInfo<PriceOnePriceConfig>> pageConfigs(OnePriceConfigFilterRequest request) {
        log.info("分页查询一口价配置: {}", request);
        
        PageInfo<PriceOnePriceConfig> pageInfo = onePricePurchaseService.pageConfigs(request);
        
        log.info("查询到一口价配置数量: {}", pageInfo.getList().size());
        
        return ApiResponse.success(pageInfo);
    }
    
    /**
     * 查询所有一口价配置
     */
    @GetMapping("/all")
    public ApiResponse<List<PriceOnePriceConfig>> getAllConfigs() {
        log.info("查询所有一口价配置");
        
        List<PriceOnePriceConfig> configs = onePricePurchaseService.getAllConfigsForAdmin();
        
        log.info("查询到一口价配置数量: {}", configs.size());
        
        return ApiResponse.success(configs);
    }
    
    /**
     * 根据ID查询一口价配置
     */
    @GetMapping("/{id}")
    public ApiResponse<PriceOnePriceConfig> getConfigById(@PathVariable Long id) {
        log.info("根据ID查询一口价配置: id={}", id);
        
        PriceOnePriceConfig config = onePricePurchaseService.getConfigById(id);
        
        if (config == null) {
            return ApiResponse.fail("配置不存在");
        }
        
        return ApiResponse.success(config);
    }
    
    /**
     * 创建一口价配置
     */
    @PostMapping("/")
    public ApiResponse<Long> createConfig(@RequestBody OnePriceConfigRequest request) {
        log.info("创建一口价配置: {}", request);
        
        // 转换为实体对象
        PriceOnePriceConfig config = new PriceOnePriceConfig();
        config.setConfigName(request.getConfigName());
        config.setScenicId(request.getScenicId());
        config.setOnePrice(request.getOnePrice());
        config.setOriginalPrice(request.getOriginalPrice());
        config.setDescription(request.getDescription());
        config.setIsActive(request.getIsActive());
        config.setStartTime(request.getStartTime());
        config.setEndTime(request.getEndTime());
        config.setCanUseCoupon(request.getCanUseCoupon());
        config.setCanUseVoucher(request.getCanUseVoucher());
        
        Long configId = onePricePurchaseService.createConfig(config);
        
        if (configId != null) {
            log.info("一口价配置创建成功: configId={}", configId);
            return ApiResponse.success(configId);
        } else {
            return ApiResponse.fail("创建一口价配置失败");
        }
    }
    
    /**
     * 更新一口价配置
     */
    @PutMapping("/{id}")
    public ApiResponse<Boolean> updateConfig(@PathVariable Long id, @RequestBody OnePriceConfigRequest request) {
        log.info("更新一口价配置: id={}, request={}", id, request);
        
        // 转换为实体对象
        PriceOnePriceConfig config = new PriceOnePriceConfig();
        config.setId(id);
        config.setConfigName(request.getConfigName());
        config.setScenicId(request.getScenicId());
        config.setOnePrice(request.getOnePrice());
        config.setOriginalPrice(request.getOriginalPrice());
        config.setDescription(request.getDescription());
        config.setIsActive(request.getIsActive());
        config.setStartTime(request.getStartTime());
        config.setEndTime(request.getEndTime());
        config.setCanUseCoupon(request.getCanUseCoupon());
        config.setCanUseVoucher(request.getCanUseVoucher());
        
        boolean success = onePricePurchaseService.updateConfig(config);
        
        if (success) {
            log.info("一口价配置更新成功: id={}", id);
            return ApiResponse.success(true);
        } else {
            return ApiResponse.fail("更新一口价配置失败");
        }
    }
    
    /**
     * 删除一口价配置
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Boolean> deleteConfig(@PathVariable Long id) {
        log.info("删除一口价配置: id={}", id);
        
        boolean success = onePricePurchaseService.deleteConfig(id);
        
        if (success) {
            log.info("一口价配置删除成功: id={}", id);
            return ApiResponse.success(true);
        } else {
            return ApiResponse.fail("删除一口价配置失败");
        }
    }
    
    /**
     * 启用/禁用一口价配置
     */
    @PutMapping("/{id}/status")
    public ApiResponse<Boolean> updateConfigStatus(@PathVariable Long id, @RequestParam Boolean isActive) {
        log.info("更新一口价配置状态: id={}, isActive={}", id, isActive);
        
        boolean success = onePricePurchaseService.updateConfigStatus(id, isActive);
        
        if (success) {
            log.info("一口价配置状态更新成功: id={}, isActive={}", id, isActive);
            return ApiResponse.success(true);
        } else {
            return ApiResponse.fail("更新一口价配置状态失败");
        }
    }
    
    /**
     * 根据景区查询启用的一口价配置
     */
    @GetMapping("/scenic/{scenicId}")
    public ApiResponse<List<PriceOnePriceConfig>> getConfigsByScenic(@PathVariable Long scenicId) {
        log.info("根据景区查询启用的一口价配置: scenicId={}", scenicId);
        
        List<PriceOnePriceConfig> configs = onePricePurchaseService.getActiveConfigsByScenic(scenicId);
        
        log.info("景区 {} 查询到一口价配置数量: {}", scenicId, configs.size());
        
        return ApiResponse.success(configs);
    }
    
    /**
     * 检查景区是否适用一口价
     */
    @GetMapping("/check/{scenicId}")
    public ApiResponse<Boolean> checkOnePriceApplicable(@PathVariable Long scenicId) {
        log.info("检查景区是否适用一口价: scenicId={}", scenicId);
        
        boolean applicable = onePricePurchaseService.isOnePriceApplicable(scenicId);
        
        log.info("景区 {} 一口价适用性: {}", scenicId, applicable);
        
        return ApiResponse.success(applicable);
    }
}