package org.dromara.ai.controller.app;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.ai.domain.dto.AiChatResponse;
import org.dromara.ai.service.AiCodingAssistantService;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * AI代码助手控制器
 * 提供代码分析和项目脚手架生成功能
 *
 * @author pengles
 */
@Slf4j
@RestController
@RequestMapping("/ai/assistant")
@RequiredArgsConstructor
public class AiAssistantController {

    private final AiCodingAssistantService assistantService;

    /**
     * AI代码分析
     *
     * @param code   代码内容
     * @param model  模型名称（可选）
     * @param userId 用户ID（可选）
     * @param params 额外参数（可选）
     * @return 分析结果
     */
    @PostMapping("/analyze")
    @Log(title = "AI代码分析", businessType = BusinessType.OTHER)
    public R<AiChatResponse> analyze(@RequestParam String code,
                                     @RequestParam(required = false) String model,
                                     @RequestParam(required = false) String userId,
                                     @RequestBody(required = false) Map<String, Object> params) {
        try {
            AiChatResponse resp = assistantService.analyzeCode(code, model, params, userId);
            return R.ok(resp);
        } catch (Exception e) {
            log.error("AI代码分析失败", e);
            return R.fail("AI代码分析失败: " + e.getMessage());
        }
    }

    /**
     * AI项目脚手架生成
     *
     * @param appType 应用类型（如SpringBoot、Vue等）
     * @param model   模型名称（可选）
     * @param userId  用户ID（可选）
     * @param options 选项参数（可选）
     * @return 生成结果
     */
    @PostMapping("/scaffold")
    @Log(title = "AI脚手架生成", businessType = BusinessType.OTHER)
    public R<AiChatResponse> scaffold(@RequestParam String appType,
                                      @RequestParam(required = false) String model,
                                      @RequestParam(required = false) String userId,
                                      @RequestBody(required = false) Map<String, Object> options) {
        try {
            AiChatResponse resp = assistantService.generateScaffold(appType, options, model, userId);
            return R.ok(resp);
        } catch (Exception e) {
            log.error("AI脚手架生成失败", e);
            return R.fail("AI脚手架生成失败: " + e.getMessage());
        }
    }
}
