package com.ruoyi.web.controller.jarvis;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jarvis.service.ISocialMediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 小红书/抖音内容生成Controller
 * 
 * @author ruoyi
 * @date 2025-01-XX
 */
@RestController
@RequestMapping("/jarvis/social-media")
public class SocialMediaController extends BaseController
{
    @Autowired
    private ISocialMediaService socialMediaService;

    /**
     * 提取关键词
     */
    @PostMapping("/extract-keywords")
    public AjaxResult extractKeywords(@RequestBody Map<String, Object> request)
    {
        try {
            String productName = (String) request.get("productName");
            if (productName == null || productName.trim().isEmpty()) {
                return AjaxResult.error("商品名称不能为空");
            }
            
            Map<String, Object> result = socialMediaService.extractKeywords(productName);
            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("提取关键词失败", e);
            return AjaxResult.error("提取关键词失败: " + e.getMessage());
        }
    }

    /**
     * 生成文案
     */
    @PostMapping("/generate-content")
    public AjaxResult generateContent(@RequestBody Map<String, Object> request)
    {
        try {
            String productName = (String) request.get("productName");
            if (productName == null || productName.trim().isEmpty()) {
                return AjaxResult.error("商品名称不能为空");
            }
            
            Object originalPriceObj = request.get("originalPrice");
            Object finalPriceObj = request.get("finalPrice");
            String keywords = (String) request.get("keywords");
            String style = (String) request.getOrDefault("style", "both");
            
            Map<String, Object> result = socialMediaService.generateContent(
                productName, originalPriceObj, finalPriceObj, keywords, style
            );
            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("生成文案失败", e);
            return AjaxResult.error("生成文案失败: " + e.getMessage());
        }
    }

    /**
     * 一键生成完整内容（关键词 + 文案 + 图片）
     */
    @Log(title = "小红书/抖音内容生成", businessType = BusinessType.OTHER)
    @PostMapping("/generate-complete")
    public AjaxResult generateComplete(@RequestBody Map<String, Object> request)
    {
        try {
            String productImageUrl = (String) request.get("productImageUrl");
            String productName = (String) request.get("productName");
            if (productName == null || productName.trim().isEmpty()) {
                return AjaxResult.error("商品名称不能为空");
            }
            
            Object originalPriceObj = request.get("originalPrice");
            Object finalPriceObj = request.get("finalPrice");
            String style = (String) request.getOrDefault("style", "both");
            
            Map<String, Object> result = socialMediaService.generateCompleteContent(
                productImageUrl, productName, originalPriceObj, finalPriceObj, style
            );
            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("生成完整内容失败", e);
            return AjaxResult.error("生成完整内容失败: " + e.getMessage());
        }
    }

    /**
     * 获取提示词模板列表
     */
    @GetMapping("/prompt/list")
    public AjaxResult listPromptTemplates()
    {
        try {
            return socialMediaService.listPromptTemplates();
        } catch (Exception e) {
            logger.error("获取提示词模板列表失败", e);
            return AjaxResult.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 获取单个提示词模板
     */
    @GetMapping("/prompt/{key}")
    public AjaxResult getPromptTemplate(@PathVariable String key)
    {
        try {
            return socialMediaService.getPromptTemplate(key);
        } catch (Exception e) {
            logger.error("获取提示词模板失败", e);
            return AjaxResult.error("获取失败: " + e.getMessage());
        }
    }

    /**
     * 保存提示词模板
     */
    @Log(title = "保存提示词模板", businessType = BusinessType.UPDATE)
    @PostMapping("/prompt/save")
    public AjaxResult savePromptTemplate(@RequestBody Map<String, Object> request)
    {
        try {
            return socialMediaService.savePromptTemplate(request);
        } catch (Exception e) {
            logger.error("保存提示词模板失败", e);
            return AjaxResult.error("保存失败: " + e.getMessage());
        }
    }

    /**
     * 删除提示词模板（恢复默认）
     */
    @Log(title = "删除提示词模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/prompt/{key}")
    public AjaxResult deletePromptTemplate(@PathVariable String key)
    {
        try {
            return socialMediaService.deletePromptTemplate(key);
        } catch (Exception e) {
            logger.error("删除提示词模板失败", e);
            return AjaxResult.error("删除失败: " + e.getMessage());
        }
    }
}

