package cn.meowrain.aioj.backend.aiservice.controller;

import cn.meowrain.aioj.backend.aiservice.dto.req.*;
import cn.meowrain.aioj.backend.aiservice.dto.resp.*;
import cn.meowrain.aioj.backend.aiservice.service.AIService;
import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * AI 服务控制器
 */
@Tag(name = "AI 服务", description = "AI 代码分析、优化、测试用例生成等接口")
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class AIController {

    private final AIService aiService;

    @PostMapping("/analyze")
    @Operation(summary = "分析代码", description = "对提交的代码进行分析，包括复杂度、性能、可读性等评分")
    public Result<AnalyzeCodeRespDTO> analyzeCode(@Valid @RequestBody AnalyzeCodeReqDTO request) {
        AnalyzeCodeRespDTO response = (AnalyzeCodeRespDTO) aiService.analyzeCode(request);
        return Results.success(response);
    }

    @PostMapping("/optimize")
    @Operation(summary = "优化代码", description = "对代码进行优化，提升性能、可读性或内存使用")
    public Result<OptimizeCodeRespDTO> optimizeCode(@Valid @RequestBody OptimizeCodeReqDTO request) {
        OptimizeCodeRespDTO response = (OptimizeCodeRespDTO) aiService.optimizeCode(request);
        return Results.success(response);
    }

    @PostMapping("/test-cases")
    @Operation(summary = "生成测试用例", description = "根据代码和问题描述自动生成测试用例")
    public Result<GenerateTestCasesRespDTO> generateTestCases(@Valid @RequestBody GenerateTestCasesReqDTO request) {
        GenerateTestCasesRespDTO response = (GenerateTestCasesRespDTO) aiService.generateTestCases(request);
        return Results.success(response);
    }

    @PostMapping("/explain")
    @Operation(summary = "解释代码", description = "对代码进行详细解释，帮助理解代码逻辑")
    public Result<ExplainCodeRespDTO> explainCode(@Valid @RequestBody ExplainCodeReqDTO request) {
        ExplainCodeRespDTO response = (ExplainCodeRespDTO) aiService.explainCode(request);
        return Results.success(response);
    }

    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查 AI 服务是否正常运行")
    public Result<String> health() {
        return Results.success("AI Service is running");
    }
}
