package com.ruoyi.web.controller.publicapi;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 评论生成 公共接口（免登录）
 */
@Anonymous
@RestController
@RequestMapping("/public/comment")
public class CommentPublicController extends BaseController {

    // TODO: 可改为读取配置
    private static final String JD_BASE = "http://192.168.8.88:6666/jd";
    private static final String SKEY = "2192057370ef8140c201079969c956a3";

    /**
     * 获取可选型号/类型（示例）
     */
    @GetMapping("/types")
    public AjaxResult types() {
        try {
            String url = JD_BASE + "/comment/types?skey=" + SKEY;
            String result = HttpUtils.sendGet(url);
            Object parsed = JSON.parse(result);
            return AjaxResult.success(parsed);
        } catch (Exception e) {
            return AjaxResult.error("types failed: " + e.getMessage());
        }
    }

    /**
     * 生成评论（示例实现：返回两条通用好评与空图片列表）
     * 入参：productType（型号/类型）
     */
    @PostMapping("/generate")
    public AjaxResult generate(@RequestBody Map<String, String> body) {
        try {
            String url = JD_BASE + "/comment/generate";
            JSONObject param = new JSONObject();
            param.put("skey", SKEY);
            if (body != null && body.get("productType") != null) {
                param.put("productType", body.get("productType"));
            }
            String result = HttpUtils.sendJsonPost(url, param.toJSONString());
            Object parsed = JSON.parse(result);
            return AjaxResult.success(parsed);
        } catch (Exception e) {
            return AjaxResult.error("generate failed: " + e.getMessage());
        }
    }

}


