package org.dromara.ai.controller.app;

import lombok.RequiredArgsConstructor;
import org.dromara.ai.provider.AiProviderManager;
import org.dromara.ai.service.SimpleAiService;
import org.dromara.common.core.domain.R;
import org.dromara.knowledge.domain.vo.KnowledgeSearchResultVo;
import org.springframework.web.bind.annotation.*;

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

/**
 * 简化的AI控制器
 * 提供基于知识库的AI问答功能
 *
 * @author pengles
 */
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class SimpleAiController {

    private final SimpleAiService simpleAiService;
    private final AiProviderManager aiProviderManager;

    /**
     * 基于知识库的问答
     *
     * @param knowledgeId 知识库ID
     * @param question    用户问题
     * @param limit       返回结果数量限制
     * @return AI回答
     */
    @PostMapping("/ask")
    public R<String> ask(@RequestParam String knowledgeId,
                         @RequestParam String question,
                         @RequestParam(required = false, defaultValue = "5") Integer limit) {
        String answer = simpleAiService.askWithKnowledge(knowledgeId, question, limit);
        return R.ok(answer);
    }

    /**
     * 向量相似度搜索
     *
     * @param knowledgeId 知识库ID
     * @param queryText   查询文本
     * @param modelName   模型名称
     * @param topK        返回前K个结果
     * @param threshold   相似度阈值
     * @return 搜索结果
     */
    @PostMapping("/search")
    public R<List<KnowledgeSearchResultVo>> vectorSearch(
            @RequestParam String knowledgeId,
            @RequestParam String queryText,
            @RequestParam(required = false, defaultValue = "default") String modelName,
            @RequestParam(required = false, defaultValue = "5") Integer topK,
            @RequestParam(required = false, defaultValue = "0.7") Double threshold) {

        List<KnowledgeSearchResultVo> results = simpleAiService.vectorSearch(
                knowledgeId, queryText, modelName, topK, threshold
        );
        return R.ok(results);
    }

    /**
     * 获取知识库统计信息
     *
     * @param knowledgeId 知识库ID
     * @return 统计信息
     */
    @GetMapping("/stats/{knowledgeId}")
    public R<Map<String, Object>> getKnowledgeStats(@PathVariable String knowledgeId) {
        Map<String, Object> stats = simpleAiService.getKnowledgeStats(knowledgeId);
        return R.ok(stats);
    }

    /**
     * 检查模型是否可用
     *
     * @param modelName 模型名称
     * @return 是否可用
     */
    @GetMapping("/model/check/{modelName}")
    public R<Boolean> checkModel(@PathVariable String modelName) {
        boolean available = simpleAiService.isModelAvailable(modelName);
        return R.ok(available);
    }

    /**
     * 获取可用的模型列表
     *
     * @return 模型列表
     */
    @GetMapping("/models")
    public R<List<String>> getAvailableModels() {
        List<String> models = simpleAiService.getAvailableModels();
        return R.ok(models);
    }

    /**
     * AI服务健康检查
     *
     * @return 健康状态与Provider统计
     */
    @GetMapping("/health")
    public R<Map<String, Object>> health() {
        Map<String, Object> stats = aiProviderManager.getAllStatistics();
        stats.put("status", "UP");
        return R.ok(stats);
    }
}
