package com.ycwl.basic.controller.pc;

import com.ycwl.basic.integration.scenic.dto.config.DefaultConfigDTO;
import com.ycwl.basic.integration.scenic.service.DefaultConfigIntegrationService;
import com.ycwl.basic.utils.ApiConst;
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/default-config")
@RequiredArgsConstructor
public class DefaultConfigController {

    private final DefaultConfigIntegrationService defaultConfigIntegrationService;

    /**
     * 获取默认配置列表
     */
    @GetMapping("/")
    public ApiResponse<List<DefaultConfigDTO>> listDefaultConfigs() {
        log.info("获取默认配置列表");
        try {
            List<DefaultConfigDTO> configs = defaultConfigIntegrationService.listDefaultConfigs();
            return ApiResponse.success(configs);
        } catch (Exception e) {
            log.error("获取默认配置列表失败", e);
            return ApiResponse.fail("获取默认配置列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据配置键获取默认配置
     */
    @GetMapping("/{configKey}")
    public ApiResponse<DefaultConfigDTO> getDefaultConfig(@PathVariable String configKey) {
        log.info("获取默认配置, configKey: {}", configKey);
        try {
            DefaultConfigDTO config = defaultConfigIntegrationService.getDefaultConfig(configKey);
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("获取默认配置失败, configKey: {}", configKey, e);
            return ApiResponse.fail("获取默认配置失败: " + e.getMessage());
        }
    }

    /**
     * 创建默认配置
     */
    @PostMapping("/")
    public ApiResponse<DefaultConfigDTO> createDefaultConfig(@RequestBody DefaultConfigDTO request) {
        log.info("创建默认配置, configKey: {}", request.getConfigKey());
        try {
            DefaultConfigDTO config = defaultConfigIntegrationService.createDefaultConfig(request);
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("创建默认配置失败, configKey: {}", request.getConfigKey(), e);
            return ApiResponse.fail("创建默认配置失败: " + e.getMessage());
        }
    }

    /**
     * 更新默认配置
     */
    @PutMapping("/{configKey}")
    public ApiResponse<DefaultConfigDTO> updateDefaultConfig(@PathVariable String configKey,
                                                           @RequestBody DefaultConfigDTO request) {
        log.info("更新默认配置, configKey: {}", configKey);
        try {
            DefaultConfigDTO config = defaultConfigIntegrationService.updateDefaultConfig(configKey, request);
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("更新默认配置失败, configKey: {}", configKey, e);
            return ApiResponse.fail("更新默认配置失败: " + e.getMessage());
        }
    }

    /**
     * 删除默认配置
     */
    @DeleteMapping("/{configKey}")
    public ApiResponse<Void> deleteDefaultConfig(@PathVariable String configKey) {
        log.info("删除默认配置, configKey: {}", configKey);
        try {
            defaultConfigIntegrationService.deleteDefaultConfig(configKey);
            return ApiResponse.buildResponse(ApiConst.Code.CODE_SUCCESS.code(), null, "删除成功");
        } catch (Exception e) {
            log.error("删除默认配置失败, configKey: {}", configKey, e);
            return ApiResponse.fail("删除默认配置失败: " + e.getMessage());
        }
    }
}