import type { Persona, ViralAnalysis } from "../domain/types";
import { mockDelay } from "./mockAi";
import { toneOf } from "./personaPrompt";

export async function mockAnalyzeViral(text: string): Promise<ViralAnalysis> {
  await mockDelay(600);
  const t = text.trim() || "（空文）";
  const hasQ = /？|\?/.test(t);
  const hasPain = /卡|煩|雷|痛|敏感|不會|怎麼/.test(t);
  return {
    hooks: hasQ
      ? "用真心疑問收尾，降低回覆門檻"
      : hasPain
        ? "先丟具體痛點，讀者有代入感"
        : "開場用生活情境，不像廣告",
    structure: "情境／痛點 → 自己經驗一句 → 邀請補充（或輕 CTA）",
    emotion: hasPain ? "共感 + 一點焦慮釋放" : "好奇／認同",
    copyable: "可複製：短段落、一個明確條件、結尾問句；勿整段抄原文",
    risks: "勿保證效果、勿硬廣、勿嘲諷留言者",
    summary: `這則之所以有互動潛力：${hasPain ? "痛點具體" : "情境清楚"}${hasQ ? " + 好回的問題" : ""}。改寫時保留結構，換成你的經驗與人設語氣。`,
  };
}

export async function mockMimicPost(sourceText: string, persona?: Persona | null): Promise<string> {
  await mockDelay(550);
  const tone = toneOf(persona);
  const clip = sourceText.trim().slice(0, 80) || "某個生活卡關";
  const name = persona?.name || "預設";
  return [
    `（仿寫 · ${name} · ${tone}）`,
    `最近也卡在類似情況：${clip}${sourceText.length > 80 ? "…" : ""}`,
    ``,
    `我自己目前是先抓「使用情境」再對規格，比較不會越研究越焦慮。`,
    `有人也這樣嗎？歡迎補一句你的實際經驗。`,
  ].join("\n");
}

export function formatViralAnalysis(a: ViralAnalysis): string {
  return [
    `【為什麼可能爆】${a.summary}`,
    `【鉤子】${a.hooks}`,
    `【結構】${a.structure}`,
    `【情緒】${a.emotion}`,
    `【可複製】${a.copyable}`,
    `【風險】${a.risks}`,
  ].join("\n");
}
