package com.ycwl.basic.controller.pc;

import com.ycwl.basic.integration.scenic.dto.config.BatchConfigRequest;
import com.ycwl.basic.integration.scenic.dto.config.BatchUpdateResponse;
import com.ycwl.basic.integration.scenic.dto.config.CreateConfigRequest;
import com.ycwl.basic.integration.scenic.dto.config.ScenicConfigV2DTO;
import com.ycwl.basic.integration.scenic.dto.config.UpdateConfigRequest;
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterPageResponse;
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
import com.ycwl.basic.integration.scenic.dto.scenic.CreateScenicRequest;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.integration.common.response.PageResponse;
import com.ycwl.basic.integration.scenic.dto.scenic.UpdateScenicRequest;
import com.ycwl.basic.integration.scenic.service.ScenicConfigIntegrationService;
import com.ycwl.basic.integration.scenic.service.ScenicIntegrationService;
import com.ycwl.basic.utils.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Author：longbinbin
 * @Date：2024/12/26
 * 景区管理 V2 版本控制器 - 基于 zt-scenic 集成服务
 */
@Slf4j
@RestController
@RequestMapping("/api/scenic/v2")
@RequiredArgsConstructor
public class ScenicV2Controller {

    private final ScenicIntegrationService scenicIntegrationService;
    private final ScenicConfigIntegrationService scenicConfigIntegrationService;

    // ========== 景区基础 CRUD 操作 ==========

    /**
     * 景区V2核心信息分页列表
     */
    @GetMapping("/")
    public ApiResponse<PageResponse<ScenicV2DTO>> listScenics(@RequestParam(defaultValue = "1") Integer page,
                                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                                        @RequestParam(required = false) Integer status,
                                                        @RequestParam(required = false) String name) {
        log.info("分页查询景区核心信息列表, page: {}, pageSize: {}, status: {}, name: {}", page, pageSize, status, name);
        
        // 参数验证：限制pageSize最大值为100
        if (pageSize > 100) {
            pageSize = 100;
        }

        try {
            PageResponse<ScenicV2DTO> response = scenicIntegrationService.listScenics(page, pageSize, status, name, null);
            return ApiResponse.success(response);
        } catch (Exception e) {
            log.error("分页查询景区核心信息列表失败", e);
            return ApiResponse.fail("分页查询景区列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询单个景区详情
     */
    @GetMapping("/{scenicId}")
    public ApiResponse<ScenicV2DTO> getScenic(@PathVariable Long scenicId) {
        log.info("查询景区详情, scenicId: {}", scenicId);
        try {
            ScenicV2DTO scenic = scenicIntegrationService.getScenic(scenicId);
            return ApiResponse.success(scenic);
        } catch (Exception e) {
            log.error("查询景区详情失败, scenicId: {}", scenicId, e);
            return ApiResponse.fail("查询景区详情失败: " + e.getMessage());
        }
    }

    /**
     * 查询景区列表（支持筛选和分页）- 高级筛选
     */
    @PostMapping("/filter")
    public ApiResponse<ScenicFilterPageResponse> filterScenics(@RequestBody @Valid ScenicFilterRequest request) {
        log.info("高级筛选景区列表, 筛选条件数: {}, 页码: {}, 页大小: {}", 
                request.getFilters().size(), request.getPage(), request.getPageSize());
        try {
            ScenicFilterPageResponse response = scenicIntegrationService.filterScenics(request);
            return ApiResponse.success(response);
        } catch (Exception e) {
            log.error("高级筛选景区列表失败", e);
            return ApiResponse.fail("高级筛选景区列表失败: " + e.getMessage());
        }
    }

    /**
     * 新增景区
     */
    @PostMapping("/create")
    public ApiResponse<ScenicV2DTO> createScenic(@RequestBody @Valid CreateScenicRequest request) {
        log.info("新增景区, name: {}, mpId: {}", request.getName(), request.getMpId());
        try {
            ScenicV2DTO scenic = scenicIntegrationService.createScenic(request);
            return ApiResponse.success(scenic);
        } catch (Exception e) {
            log.error("新增景区失败, name: {}", request.getName(), e);
            return ApiResponse.fail("新增景区失败: " + e.getMessage());
        }
    }

    /**
     * 修改景区
     */
    @PutMapping("/{scenicId}")
    public ApiResponse<ScenicV2DTO> updateScenic(@PathVariable Long scenicId, 
                                                @RequestBody @Valid UpdateScenicRequest request) {
        log.info("修改景区, scenicId: {}", scenicId);
        try {
            ScenicV2DTO scenic = scenicIntegrationService.updateScenic(scenicId, request);
            return ApiResponse.success(scenic);
        } catch (Exception e) {
            log.error("修改景区失败, scenicId: {}", scenicId, e);
            return ApiResponse.fail("修改景区失败: " + e.getMessage());
        }
    }

    /**
     * 删除景区
     */
    @DeleteMapping("/{scenicId}")
    public ApiResponse<Void> deleteScenic(@PathVariable Long scenicId) {
        log.info("删除景区, scenicId: {}", scenicId);
        try {
            scenicIntegrationService.deleteScenic(scenicId);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("删除景区失败, scenicId: {}", scenicId, e);
            return ApiResponse.fail("删除景区失败: " + e.getMessage());
        }
    }

    /**
     * 景区列表查询（默认1000条）
     * 只支持根据状态筛选
     */
    @GetMapping("/list")
    public ApiResponse<PageResponse<ScenicV2DTO>> listScenicsByStatus(@RequestParam(required = false) Integer status) {
        log.info("查询景区列表, status: {}", status);
        try {
            // 默认查询1000条数据，第1页
            PageResponse<ScenicV2DTO> scenics = scenicIntegrationService.listScenics(1, 1000, status, null, null);
            return ApiResponse.success(scenics);
        } catch (Exception e) {
            log.error("查询景区列表失败, status: {}", status, e);
            return ApiResponse.fail("查询景区列表失败: " + e.getMessage());
        }
    }


    // ========== 景区配置管理 ==========

    /**
     * 获取景区配置列表
     */
    @GetMapping("/{scenicId}/config")
    public ApiResponse<List<ScenicConfigV2DTO>> listConfigs(@PathVariable Long scenicId) {
        log.info("获取景区配置列表, scenicId: {}", scenicId);
        try {
            List<ScenicConfigV2DTO> configs = scenicConfigIntegrationService.listConfigs(scenicId);
            return ApiResponse.success(configs);
        } catch (Exception e) {
            log.error("获取景区配置列表失败, scenicId: {}", scenicId, e);
            return ApiResponse.fail("获取景区配置列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据配置键获取配置
     */
    @GetMapping("/{scenicId}/config/{configKey}")
    public ApiResponse<ScenicConfigV2DTO> getConfigByKey(@PathVariable Long scenicId, 
                                                        @PathVariable String configKey) {
        log.info("根据键获取景区配置, scenicId: {}, configKey: {}", scenicId, configKey);
        try {
            ScenicConfigV2DTO config = scenicConfigIntegrationService.getConfigByKey(scenicId, configKey);
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("根据键获取景区配置失败, scenicId: {}, configKey: {}", scenicId, configKey, e);
            return ApiResponse.fail("获取配置失败: " + e.getMessage());
        }
    }

    /**
     * 创建景区配置
     */
    @PostMapping("/{scenicId}/config")
    public ApiResponse<ScenicConfigV2DTO> createConfig(@PathVariable Long scenicId, 
                                                      @RequestBody @Valid CreateConfigRequest request) {
        log.info("创建景区配置, scenicId: {}, configKey: {}", scenicId, request.getConfigKey());
        try {
            ScenicConfigV2DTO config = scenicConfigIntegrationService.createConfig(scenicId, request);
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("创建景区配置失败, scenicId: {}, configKey: {}", scenicId, request.getConfigKey(), e);
            return ApiResponse.fail("创建配置失败: " + e.getMessage());
        }
    }

    /**
     * 更新景区配置
     */
    @PutMapping("/{scenicId}/config/{configId}")
    public ApiResponse<ScenicConfigV2DTO> updateConfig(@PathVariable Long scenicId, 
                                                      @PathVariable String configId,
                                                      @RequestBody @Valid UpdateConfigRequest request) {
        log.info("更新景区配置, scenicId: {}, configId: {}", scenicId, configId);
        try {
            ScenicConfigV2DTO config = scenicConfigIntegrationService.updateConfig(scenicId, configId, request);
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("更新景区配置失败, scenicId: {}, configId: {}", scenicId, configId, e);
            return ApiResponse.fail("更新配置失败: " + e.getMessage());
        }
    }

    /**
     * 删除景区配置
     */
    @DeleteMapping("/{scenicId}/config/{configId}")
    public ApiResponse<Void> deleteConfig(@PathVariable Long scenicId, @PathVariable String configId) {
        log.info("删除景区配置, scenicId: {}, configId: {}", scenicId, configId);
        try {
            scenicConfigIntegrationService.deleteConfig(scenicId, configId);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("删除景区配置失败, scenicId: {}, configId: {}", scenicId, configId, e);
            return ApiResponse.fail("删除配置失败: " + e.getMessage());
        }
    }

    /**
     * 批量更新景区配置
     */
    @PutMapping("/{scenicId}/config/batch")
    public ApiResponse<BatchUpdateResponse> batchUpdateConfigs(@PathVariable Long scenicId, 
                                                             @RequestBody @Valid BatchConfigRequest request) {
        log.info("批量更新景区配置, scenicId: {}, configs count: {}", scenicId, request.getConfigs().size());
        try {
            BatchUpdateResponse response = scenicConfigIntegrationService.batchUpdateConfigs(scenicId, request);
            return ApiResponse.success(response);
        } catch (Exception e) {
            log.error("批量更新景区配置失败, scenicId: {}", scenicId, e);
            return ApiResponse.fail("批量更新配置失败: " + e.getMessage());
        }
    }
}