package com.dora.controller;

import com.dora.common.Result;
import com.dora.dto.AiModelDto;
import com.dora.service.AiModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI模型查询控制器（用户端）
 */
@Slf4j
@RestController
@RequestMapping("/user/ai/models")
@Tag(name = "AI模型查询（用户端）", description = "用户端AI模型列表查询接口")
@RequiredArgsConstructor
public class AiModelController {

    private final AiModelService aiModelService;

    /**
     * 获取所有可用模型列表（支持筛选）
     */
    @GetMapping
    @Operation(summary = "获取模型列表", description = "获取所有可用的AI模型列表，支持按任务类型和厂商筛选")
    public Result<List<AiModelDto.ModelInfoResponse>> getAllModels(
            @Parameter(description = "任务类型（可选：image-图片/video-视频/audio-音频/text-文本）", example = "image")
            @RequestParam(required = false) String taskType,
            @Parameter(description = "服务提供商（可选：openai/runninghub）", example = "openai")
            @RequestParam(required = false) String provider,
            @Parameter(description = "是否只返回已启用的模型", example = "true")
            @RequestParam(required = false, defaultValue = "true") Boolean enabledOnly) {
        try {
            List<AiModelDto.ModelInfoResponse> models = 
                    aiModelService.getAllModels(taskType, provider, enabledOnly);
            return Result.success(models);
        } catch (Exception e) {
            log.error("获取模型列表失败", e);
            return Result.error("获取模型列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取按任务类型分组的模型列表
     */
    @GetMapping("/group-by-type")
    @Operation(summary = "按类型分组获取模型", description = "获取按任务类型分组的AI模型列表")
    public Result<List<AiModelDto.ModelsByTypeResponse>> getModelsByType(
            @Parameter(description = "服务提供商（可选：openai/runninghub）", example = "")
            @RequestParam(required = false) String provider,
            @Parameter(description = "是否只返回已启用的模型", example = "true")
            @RequestParam(required = false, defaultValue = "true") Boolean enabledOnly) {
        try {
            List<AiModelDto.ModelsByTypeResponse> models = 
                    aiModelService.getModelsByType(provider, enabledOnly);
            return Result.success(models);
        } catch (Exception e) {
            log.error("获取按类型分组的模型列表失败", e);
            return Result.error("获取模型列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取按厂商分组的模型列表
     */
    @GetMapping("/group-by-provider")
    @Operation(summary = "按厂商分组获取模型", description = "获取按服务提供商分组的AI模型列表")
    public Result<List<AiModelDto.ModelsByProviderResponse>> getModelsByProvider(
            @Parameter(description = "任务类型（可选：image-图片/video-视频/audio-音频/text-文本）", example = "")
            @RequestParam(required = false) String taskType,
            @Parameter(description = "是否只返回已启用的模型", example = "true")
            @RequestParam(required = false, defaultValue = "true") Boolean enabledOnly) {
        try {
            List<AiModelDto.ModelsByProviderResponse> models = 
                    aiModelService.getModelsByProvider(taskType, enabledOnly);
            return Result.success(models);
        } catch (Exception e) {
            log.error("获取按厂商分组的模型列表失败", e);
            return Result.error("获取模型列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取模型统计信息
     */
    @GetMapping("/stats")
    @Operation(summary = "获取模型统计", description = "获取系统中AI模型的统计信息")
    public Result<AiModelDto.ModelStatsResponse> getModelStats() {
        try {
            AiModelDto.ModelStatsResponse stats = aiModelService.getModelStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取模型统计失败", e);
            return Result.error("获取模型统计失败：" + e.getMessage());
        }
    }
}

