/**
 * llm-intent.js
 *
 * LLM 意图解析网关。
 * 接收客服回复文本 + 当前轮次，通过 LLM 做意图分类或实体提取。
 *
 * 支持 OpenAI 兼容接口 (覆盖 DeepSeek / 智谱 / 通义等)。
 *
 * 环境变量:
 *   CHATBOT_INTENT_LLM_PROVIDER + 对应 Provider 凭证，或 legacy LLM_API_KEY/LLM_BASE_URL
 *   CHATBOT_INTENT_LLM_MODEL    客服意图阶段模型，默认回退 LLM_MODEL
 *   LLM_MODEL                   全局回退模型，缺省 deepseek-v4-flash
 */

import process from "node:process";
import { getRoundConfig } from "./chat-templates.js";
import {
  renderPromptTemplate,
  resolveTextPrompt,
} from "../../automation/internal/prompt-runtime.js";
import {
  resolveStageModelFromEnv,
  resolveStageProviderFromEnv,
  resolveStageRuntimeFromEnv,
} from "../../automation/core/llm-env.js";

// ── 配置 ─────────────────────────────────────────────

export function resolveChatbotIntentModel(env = process.env) {
  return resolveStageModelFromEnv("chatbot_intent", env);
}

function getLLMConfig() {
  const runtime = resolveStageRuntimeFromEnv("chatbot_intent", process.env);
  if (!runtime) {
    throw new Error("缺少客服意图 LLM 配置。请设置 CHATBOT_INTENT_LLM_PROVIDER 及对应 provider key/base url，或 legacy LLM_API_KEY/LLM_BASE_URL。");
  }
  return {
    apiKey: runtime.apiKey,
    baseUrl: runtime.baseUrl,
    model: runtime.model,
    provider: runtime.provider,
  };
}

const CHATBOT_PROMPT_KEYS = {
  1: "chatbot.intent.round_1.system",
  2: "chatbot.intent.round_2.system",
  3: "chatbot.intent.round_3.system",
  4: "chatbot.intent.round_4.system",
  generic: "chatbot.intent.generic_round.system",
};

export function getPromptScope() {
  return {
    provider: process.env.PROMPT_PROVIDER || resolveStageProviderFromEnv("chatbot_intent", process.env) || process.env.LLM_PROVIDER || "*",
    model: resolveChatbotIntentModel(),
  };
}

export function buildGenericRoundSystemPrompt(currentRound, roundConfig, template) {
  return renderPromptTemplate(template, {
    current_round: currentRound,
    round_name: roundConfig.name,
    template_text: roundConfig.templateText,
    pass_condition: roundConfig.passCondition || "",
  });
}

export async function buildSystemPrompt(currentRound) {
  const roundConfig = getRoundConfig(currentRound);
  const promptKey = CHATBOT_PROMPT_KEYS[currentRound] ?? CHATBOT_PROMPT_KEYS.generic;
  const prompt = await resolveTextPrompt(promptKey, {
    ...getPromptScope(),
  });
  const template = prompt.text;

  if (!CHATBOT_PROMPT_KEYS[currentRound]) {
    return buildGenericRoundSystemPrompt(currentRound, roundConfig, template);
  }

  return renderPromptTemplate(template, {
    round_name: roundConfig.name,
    template_text: roundConfig.templateText,
    pass_condition: roundConfig.passCondition || "",
  });
}

// ── LLM 调用 ─────────────────────────────────────────

/**
 * 调用 OpenAI 兼容 Chat Completion API
 *
 * @param {string} systemPrompt
 * @param {string} userMessage
 * @returns {Promise<string>} 模型输出的文本
 */
export async function callLLM(systemPrompt, userMessage) {
  const config = getLLMConfig();
  console.log(`  🤖 [LLM] 使用客服意图模型: ${config.model}`);

  const response = await fetch(`${config.baseUrl}/chat/completions`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${config.apiKey}`,
    },
    body: JSON.stringify({
      model: config.model,
      messages: [
        { role: "system", content: systemPrompt },
        { role: "user", content: userMessage },
      ],
      temperature: 0.1,
      max_tokens: 512,
    }),
  });

  if (!response.ok) {
    const body = await response.text().catch(() => "");
    throw new Error(`LLM API 调用失败: HTTP ${response.status} - ${body.slice(0, 200)}`);
  }

  const data = await response.json();
  return data.choices?.[0]?.message?.content ?? "";
}

/**
 * 解析 LLM 输出的 JSON（容错：处理 markdown 代码块包裹）
 */
function parseLLMJson(raw) {
  let cleaned = raw.trim();
  // 去掉 ```json ... ``` 包裹
  if (cleaned.startsWith("```")) {
    cleaned = cleaned.replace(/^```(?:json)?\s*/, "").replace(/\s*```$/, "");
  }
  return JSON.parse(cleaned);
}

// ── 公共入口 ─────────────────────────────────────────

/**
 * 分析客服回复
 *
 * @param {string} replyText    客服回复文本
 * @param {number} currentRound 当前轮次 (1-N)
 * @param {object} [context]    可选的上下文 (材料名/规格等，附加到 user message)
 * @returns {Promise<object>}   各轮次对应的结构化结果
 */
export async function analyzeReply(replyText, currentRound, context = {}) {
  const systemPrompt = await buildSystemPrompt(currentRound);

  // 构造 user message，附加上下文
  let userMessage = `客服回复内容：\n"${replyText}"`;
  const itemName = context.standardized_name || context.original_name || context.material_name || context.title;
  if (itemName) {
    userMessage += `\n\n参考信息：商品名称=${itemName}`;
    if (context.standardized_spec || context.original_spec || context.specification) {
      userMessage += `，规格=${context.standardized_spec || context.original_spec || context.specification}`;
    }
    if (context.quantity) userMessage += `，需求量=${context.quantity}${context.standardized_unit || context.unit || ""}`;
    if (context.brand) userMessage += `，品牌=${context.brand}`;
  }

  console.log(`  🤖 [LLM] 分析第 ${currentRound} 轮回复，模型=${resolveChatbotIntentModel()}...`);

  const raw = await callLLM(systemPrompt, userMessage);
  console.log(`  🤖 [LLM] 原始输出: ${raw.slice(0, 120)}`);

  try {
    const parsed = parseLLMJson(raw);
    return { ...parsed, _raw: raw };
  } catch {
    console.error(`  ⚠️ [LLM] JSON 解析失败，原始输出: ${raw}`);
    // 解析失败 → 保守处理为 PENDING（人工接管）
    return {
      passed: false,
      status: "PENDING",
      reason: `LLM 输出解析失败: ${raw.slice(0, 100)}`,
      _raw: raw,
    };
  }
}
