package com.kexue.skills.controller;

import com.kexue.skills.common.CommonResult;
import com.kexue.skills.entity.CmsContent;
import com.kexue.skills.entity.dto.YamlContentDto;
import com.kexue.skills.entity.request.GenIntroduceRequest;
import com.kexue.skills.entity.request.SkillGenRequest;
import com.kexue.skills.entity.request.SkillPreGenRequest;
import com.kexue.skills.entity.request.SkillUploadDto;
import com.kexue.skills.entity.response.SkillResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 技能生成控制器
 *
 * @author 维哥
 * @since 2026-01-28
 */
@RestController
@RequestMapping("api/skillGen")
@Tag(name = "技能生成 Api")
@CrossOrigin(origins = "*")
public class SkillGenController {

    @Resource
    private com.kexue.skills.service.SkillGenService skillGenService;

    /**
     * 生成技能
     *
     * @param request 生成请求
     * @return 生成结果
     */
    @PostMapping("/preGenerate")
    @Operation(summary = "预生成技能", description = "生成技能")
    public CommonResult<SkillResponse> preGenerate(@RequestBody SkillPreGenRequest request) {
        return CommonResult.success(skillGenService.preGenerateV2(request));
    }

    @PostMapping("/generate")
    @Operation(summary = "生成技能", description = "生成技能")
    public CommonResult<CmsContent> generate(@RequestBody SkillGenRequest request) {
        return CommonResult.success(skillGenService.generate(request));
    }

    /**
     * 分析技能
     *
     * @param request 分析请求
     * @return 分析结果
     */
    @PostMapping("/analyze")
    @Operation(summary = "分析技能", description = "分析技能")
    public CommonResult<String> analyze(@RequestBody com.kexue.skills.entity.request.SkillAnalyzeRequest request) {
        return CommonResult.success(skillGenService.analyzeSkill(request));
    }

    /**
     * 生成技能介绍
     *
     * @param request 技能内容
     * @return 技能介绍
     */
    @PostMapping("/genIntroduce")
    @Operation(summary = "生成技能介绍", description = "生成技能介绍")
    public CommonResult<String> genIntroduce(@RequestBody GenIntroduceRequest request) {
        return CommonResult.success(skillGenService.genIntroduce(request.getContent()));
    }
    
    /**
     * 根据技能描述生成技能介绍
     *
     * @param request 技能描述
     * @return 技能介绍
     */
    @PostMapping("/genIntroduceByDescription")
    @Operation(summary = "根据技能描述生成技能介绍", description = "根据技能描述生成技能介绍")
    public CommonResult<String> genIntroduceByDescription(@RequestBody GenIntroduceRequest request) {
        return CommonResult.success(skillGenService.genIntroduceByDescription(request.getContent()));
    }
    
    /**
     * 上传技能压缩包
     *
     * @param skillUploadDto 技能压缩包URL
     * @return 生成的技能内容
     */
    @PostMapping("/uploadSkill")
    @Operation(summary = "上传技能压缩包", description = "上传技能压缩包并生成技能")
    public CommonResult<CmsContent> uploadSkill(@RequestBody SkillUploadDto skillUploadDto) {
        return CommonResult.success(skillGenService.uploadSkill(skillUploadDto.getUrl()));
    }
    
    /**
     * 上传本地技能压缩包V2
     *
     * @param file 技能压缩包文件
     * @return 生成的技能内容
     */
    @PostMapping("/uploadSkillV2")
    @Operation(summary = "上传本地技能压缩包V2", description = "上传本地zip或rar文件并生成技能")
    public CommonResult<CmsContent> uploadSkillV2(
            @RequestParam("file") MultipartFile file) {
        try {
            byte[] fileBytes = file.getBytes();
            String fileName = file.getOriginalFilename();
            CmsContent cmsContent = skillGenService.uploadSkillV2(fileBytes, fileName);
            return CommonResult.success(cmsContent);
        } catch (Exception e) {
            return CommonResult.failed("上传失败：" + e.getMessage());
        }
    }
    /**
     * 上传本地技能压缩包V3 直接传入yamlContent
     *
     * @param yamlContentDto 技能压缩包文件
     * @return 生成的技能内容
     */
    @PostMapping("/uploadSkillV3")
    @Operation(summary = "上传本地技能压缩包V3,直接传入yamlContent", description = "直接传入yamlContent")
    public CommonResult<CmsContent> uploadSkillV3(@RequestBody YamlContentDto yamlContentDto) {
        CmsContent cmsContent = skillGenService.uploadSkillV3(yamlContentDto.getYamlContent());
        return CommonResult.success(cmsContent);
    }

    /**
     * 上传本地技能压缩包V2
     *
     * @param file 技能压缩包文件
     * @return 生成的技能内容
     */
    @PostMapping("/uploadSkillV4")
    @Operation(summary = "上传本地技能压缩包V2", description = "上传本地zip或rar文件并生成技能")
    public CommonResult<CmsContent> uploadSkillV4(
            @RequestParam("file") MultipartFile file) {
        try {
            byte[] fileBytes = file.getBytes();
            String fileName = file.getOriginalFilename();
            CmsContent cmsContent = skillGenService.uploadSkillV4(fileBytes, fileName);
            return CommonResult.success(cmsContent);
        } catch (Exception e) {
            return CommonResult.failed("上传失败：" + e.getMessage());
        }
    }

}
