package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.aiconfig.*;
import com.emotion.dto.response.aiconfig.AiConfigResponse;
import com.emotion.service.AiConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI配置控制器
 * 
 * @author system
 * @date 2025-10-30
 */
@RestController
@RequestMapping("/aiConfig")
@Tag(name = "AI配置管理", description = "AI配置的增删改查功能")
public class AiConfigController {

    @Autowired
    private AiConfigService aiConfigService;

    /**
     * 分页查询AI配置
     */
    @Operation(summary = "分页查询AI配置", description = "分页查询AI配置列表")
    @GetMapping("/page")
    public Result<PageResult<AiConfigResponse>> getPage(@Validated AiConfigPageRequest request) {
        PageResult<AiConfigResponse> pageResult = aiConfigService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取AI配置
     */
    @Operation(summary = "根据ID获取AI配置", description = "根据ID获取AI配置详情")
    @GetMapping("/detail")
    public Result<AiConfigResponse> getById(@RequestParam String id) {
        AiConfigResponse response = aiConfigService.getAiConfigResponseById(id);
        if (response == null) {
            return Result.notFound("AI配置不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建AI配置
     */
    @Operation(summary = "创建AI配置", description = "创建新的AI配置")
    @PostMapping("/create")
    public Result<AiConfigResponse> create(@RequestBody @Validated AiConfigCreateRequest request) {
        AiConfigResponse response = aiConfigService.createAiConfigWithResponse(request);
        if (response == null) {
            return Result.error("创建失败");
        }
        return Result.success("创建成功", response);
    }

    /**
     * 更新AI配置
     */
    @Operation(summary = "更新AI配置", description = "更新指定AI配置")
    @PutMapping("/update")
    public Result<AiConfigResponse> update(@RequestBody @Validated AiConfigUpdateRequest request) {
        AiConfigResponse response = aiConfigService.updateAiConfigWithResponse(request);
        if (response == null) {
            return Result.error("更新失败");
        }
        return Result.success("更新成功", response);
    }

    /**
     * 删除AI配置
     */
    @Operation(summary = "删除AI配置", description = "删除指定AI配置")
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = aiConfigService.removeById(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 根据配置类型查询AI配置
     */
    @Operation(summary = "根据配置类型查询AI配置", description = "根据配置类型查询AI配置列表")
    @GetMapping("/byConfigType")
    public Result<List<AiConfigResponse>> getByConfigType(@RequestParam String configType) {
        List<AiConfigResponse> responses = aiConfigService.getByConfigTypeWithResponse(configType);
        return Result.success(responses);
    }

    /**
     * 根据服务提供商查询AI配置
     */
    @Operation(summary = "根据服务提供商查询AI配置", description = "根据服务提供商查询AI配置列表")
    @GetMapping("/byProvider")
    public Result<List<AiConfigResponse>> getByProvider(@RequestParam String provider) {
        List<AiConfigResponse> responses = aiConfigService.getByProviderWithResponse(provider);
        return Result.success(responses);
    }

    /**
     * 根据使用场景查询AI配置
     */
    @Operation(summary = "根据使用场景查询AI配置", description = "根据使用场景查询AI配置列表")
    @GetMapping("/byUsageScenario")
    public Result<List<AiConfigResponse>> getByUsageScenario(@RequestParam String usageScenario) {
        List<AiConfigResponse> responses = aiConfigService.getByUsageScenarioWithResponse(usageScenario);
        return Result.success(responses);
    }

    /**
     * 根据环境查询AI配置
     */
    @Operation(summary = "根据环境查询AI配置", description = "根据环境查询AI配置列表")
    @GetMapping("/byEnvironment")
    public Result<List<AiConfigResponse>> getByEnvironment(@RequestParam String environment) {
        List<AiConfigResponse> responses = aiConfigService.getByEnvironmentWithResponse(environment);
        return Result.success(responses);
    }

    /**
     * 查询已启用的AI配置
     */
    @Operation(summary = "查询已启用的AI配置", description = "查询已启用的AI配置列表")
    @GetMapping("/enabled")
    public Result<List<AiConfigResponse>> getEnabledConfigs() {
        List<AiConfigResponse> responses = aiConfigService.getEnabledConfigsWithResponse();
        return Result.success(responses);
    }

    /**
     * 查询已禁用的AI配置
     */
    @Operation(summary = "查询已禁用的AI配置", description = "查询已禁用的AI配置列表")
    @GetMapping("/disabled")
    public Result<List<AiConfigResponse>> getDisabledConfigs() {
        List<AiConfigResponse> responses = aiConfigService.getDisabledConfigsWithResponse();
        return Result.success(responses);
    }

    /**
     * 查询默认配置
     */
    @Operation(summary = "查询默认配置", description = "查询默认配置列表")
    @GetMapping("/default")
    public Result<List<AiConfigResponse>> getDefaultConfigs() {
        List<AiConfigResponse> responses = aiConfigService.getDefaultConfigsWithResponse();
        return Result.success(responses);
    }

    /**
     * 根据配置键值查询AI配置
     */
    @Operation(summary = "根据配置键值查询AI配置", description = "根据配置键值查询AI配置")
    @GetMapping("/byConfigKey")
    public Result<AiConfigResponse> getByConfigKey(@RequestParam String configKey) {
        AiConfigResponse response = aiConfigService.getByConfigKeyWithResponse(configKey);
        if (response == null) {
            return Result.notFound("AI配置不存在");
        }
        return Result.success(response);
    }

    /**
     * 启用AI配置
     */
    @Operation(summary = "启用AI配置", description = "启用指定AI配置")
    @PutMapping("/enable")
    public Result<Void> enableConfig(@RequestParam String id) {
        boolean enabled = aiConfigService.enableConfig(id);
        if (!enabled) {
            return Result.error("启用失败");
        }
        return Result.success();
    }

    /**
     * 禁用AI配置
     */
    @Operation(summary = "禁用AI配置", description = "禁用指定AI配置")
    @PutMapping("/disable")
    public Result<Void> disableConfig(@RequestParam String id) {
        boolean disabled = aiConfigService.disableConfig(id);
        if (!disabled) {
            return Result.error("禁用失败");
        }
        return Result.success();
    }

    /**
     * 设置为默认配置
     */
    @Operation(summary = "设置为默认配置", description = "设置指定AI配置为默认配置")
    @PutMapping("/setDefault")
    public Result<Void> setAsDefault(@RequestParam String id) {
        boolean set = aiConfigService.setAsDefault(id);
        if (!set) {
            return Result.error("设置默认配置失败");
        }
        return Result.success();
    }

    /**
     * 取消默认配置
     */
    @Operation(summary = "取消默认配置", description = "取消指定AI配置的默认设置")
    @PutMapping("/unsetDefault")
    public Result<Void> unsetDefault(@RequestParam String id) {
        boolean unset = aiConfigService.unsetDefault(id);
        if (!unset) {
            return Result.error("取消默认配置失败");
        }
        return Result.success();
    }

    /**
     * 根据使用场景和环境查询最优配置
     */
    @Operation(summary = "查询最优配置", description = "根据使用场景和环境查询最优配置")
    @GetMapping("/bestConfig")
    public Result<AiConfigResponse> getBestConfig(@RequestParam String usageScenario, 
                                                  @RequestParam String environment) {
        AiConfigResponse response = aiConfigService.getBestConfigWithResponse(usageScenario, environment);
        if (response == null) {
            return Result.notFound("未找到匹配的AI配置");
        }
        return Result.success(response);
    }

    /**
     * 统计已启用配置数量
     */
    @Operation(summary = "统计已启用配置数量", description = "统计已启用配置数量")
    @GetMapping("/countEnabled")
    public Result<Long> countEnabledConfigs() {
        Long count = aiConfigService.countEnabledConfigs();
        return Result.success(count);
    }

    /**
     * 统计已禁用配置数量
     */
    @Operation(summary = "统计已禁用配置数量", description = "统计已禁用配置数量")
    @GetMapping("/countDisabled")
    public Result<Long> countDisabledConfigs() {
        Long count = aiConfigService.countDisabledConfigs();
        return Result.success(count);
    }

    /**
     * 统计默认配置数量
     */
    @Operation(summary = "统计默认配置数量", description = "统计默认配置数量")
    @GetMapping("/countDefault")
    public Result<Long> countDefaultConfigs() {
        Long count = aiConfigService.countDefaultConfigs();
        return Result.success(count);
    }

    /**
     * 根据配置类型统计数量
     */
    @Operation(summary = "根据配置类型统计数量", description = "根据配置类型统计数量")
    @GetMapping("/countByConfigType")
    public Result<Long> countByConfigType(@RequestParam String configType) {
        Long count = aiConfigService.countByConfigType(configType);
        return Result.success(count);
    }

    /**
     * 根据服务提供商统计数量
     */
    @Operation(summary = "根据服务提供商统计数量", description = "根据服务提供商统计数量")
    @GetMapping("/countByProvider")
    public Result<Long> countByProvider(@RequestParam String provider) {
        Long count = aiConfigService.countByProvider(provider);
        return Result.success(count);
    }

    /**
     * 测试后更新AI配置
     */
    @Operation(summary = "测试后更新AI配置", description = "从测试请求中解析参数并更新配置")
    @PutMapping("/updateFromTest")
    public Result<AiConfigResponse> updateFromTest(@RequestBody @Validated AiConfigTestUpdateRequest request) {
        AiConfigResponse response = aiConfigService.updateFromTestRequest(request);
        if (response == null) {
            return Result.error("更新失败，配置不存在");
        }
        return Result.success("更新成功", response);
    }
}