import type { ExpertDefinition } from "@qjclaw/shared-types"
import type { MessageListMessage } from "../chat/MessageList"
import type { ExpertGuideContent, ExpertKey, ExpertStarterPrompt } from "./ExpertsView"
import type { ExpertProject, ExpertVisualKey } from "../shell/ExpertTree"

export interface VideoStatusCardContent {
  title: string
  meta?: string
  hint?: string
}

const XIAOHONGSHU_STARTER_PROMPTS: ExpertStarterPrompt[] = [
  {
    title: "帮我做一篇推荐平价火锅的笔记",
    description: "适合餐饮探店、门店种草和收藏型内容。",
    prompt: "帮我做一篇推荐平价火锅的笔记"
  },
  {
    title: "做一篇制作抹茶奶酪欧包教程的爆文",
    description: "适合教程结构、标题钩子和步骤拆解。",
    prompt: "做一篇制作抹茶奶酪欧包教程的爆文"
  },
  {
    title: "给通勤女生做一篇春季穿搭笔记",
    description: "适合人群细分、场景搭配和封面建议。",
    prompt: "给通勤女生做一篇春季穿搭笔记"
  },
  {
    title: "做一篇针对年轻女性推荐防晒霜的爆款图文",
    description: "适合美妆护肤种草、卖点提炼和图文结构。",
    prompt: "做一篇针对年轻女性推荐防晒霜的爆款图文"
  }
]

export function getProjectDisplayName(project: ExpertProject | undefined, defaultChatName = "千匠问天"): string {
  return project?.displayName ?? project?.name ?? defaultChatName
}

export function resolveExpertKey(project: ExpertProject | undefined): ExpertKey {
  const seed = [project?.platform, project?.displayName, project?.name, project?.id]
    .filter(Boolean)
    .join(" ")
    .toLowerCase()

  if (/xiaohongshu|xhs|rednote|小红书/.test(seed)) {
    return "xiaohongshu"
  }
  if (/douyin|tiktok|抖音/.test(seed)) {
    return "douyin"
  }
  if (/browser|automation|chrome|playwright|web|浏览器|自动化/.test(seed)) {
    return "browser"
  }
  return "general"
}

export function resolveExpertVisualKey(project?: ExpertProject, definition?: ExpertDefinition): ExpertVisualKey {
  const seed = [definition?.id, definition?.name, project?.platform, project?.displayName, project?.name, project?.id]
    .filter(Boolean)
    .join(" ")
    .toLowerCase()

  if (/xiaohongshu|xhs|rednote|小红书/.test(seed)) {
    return "xiaohongshu"
  }
  if (/douyin|抖音/.test(seed)) {
    return "douyin"
  }
  if (/tiktok/.test(seed)) {
    return "tiktok"
  }
  if (/wechat|weixin|公众号|微信/.test(seed)) {
    return "wechat"
  }
  if (/zhihu|知乎/.test(seed)) {
    return "zhihu"
  }
  if (/content-account|planner|账号规划|内容账号规划/.test(seed)) {
    return "planner"
  }
  if (/precision-leads|线索|lead/.test(seed)) {
    return "leads"
  }
  if (/sales-champion|销售冠军|销冠|sales champion/.test(seed)) {
    return "sales"
  }
  if (/poster|海报/.test(seed)) {
    return "poster"
  }
  if (/(^|[\s-])x($|[\s-])|twitter/.test(seed)) {
    return "x"
  }
  if (/geo/.test(seed)) {
    return "geo"
  }
  if (/browser|automation|chrome|playwright|web|浏览器|自动化/.test(seed)) {
    return "browser"
  }
  return "general"
}

export function buildDouyinVideoStatusCard(message: MessageListMessage, expertKey: ExpertKey): VideoStatusCardContent | null {
  if (expertKey !== "douyin" || message.role !== "assistant" || message.streamState !== "streaming" || message.content.trim()) {
    return null
  }

  const title = message.statusLabel?.trim() ?? ""
  const detail = message.statusDetail?.trim() ?? ""
  const seed = `${title} ${detail}`.toLowerCase()
  const looksLikeVideoGeneration = /视频|口播|omnihuman|seedance|远端|生成|分钟|引擎|预计/.test(seed)
  if (!looksLikeVideoGeneration) {
    return null
  }

  const [meta, eta, hint] = detail.split(" | ").map((item) => item.trim()).filter(Boolean)
  const metaParts = [meta, eta].filter(Boolean)
  return {
    title: title || "视频正在生成中...",
    meta: metaParts.length ? metaParts.join(" · ") : undefined,
    hint: hint || "已进入长任务生成阶段，客户端会持续刷新状态。"
  }
}

function getExpertGuide(project: ExpertProject | undefined): ExpertGuideContent {
  switch (resolveExpertKey(project)) {
    case "xiaohongshu":
      return {
        greeting: "准备好了，开始种草",
        summary: "说清产品、场景、人群和目标，我会先拆选题、标题、笔记结构和配图方向。",
        prompts: XIAOHONGSHU_STARTER_PROMPTS.map((item) => item.prompt),
        starterPrompts: XIAOHONGSHU_STARTER_PROMPTS
      }
    case "douyin":
      return {
        greeting: "你可以直接说目标，我会按抖音内容和发布节奏来拆解。",
        summary: "适合抖音脚本策划、短视频结构、口播优化和发布执行建议。",
        prompts: [
          "帮我规划 5 条抖音短视频选题，目标是引流到私域。",
          "把这个产品介绍改成 30 秒抖音口播脚本。",
          "给我一个抖音视频的镜头脚本和字幕节奏。",
          "分析这个选题为什么更适合抖音而不是小红书。"
        ]
      }
    case "browser":
      return {
        greeting: "适合处理浏览器自动化、采集、表单填写和发布流程设计。",
        summary: "围绕浏览器操作自动化、信息采集、流程编排和执行前检查提供帮助。",
        prompts: [
          "帮我设计一个浏览器自动采集选题的执行流程。",
          "梳理抖音发布前需要自动检查的页面步骤。",
          "如果要自动登录并抓取页面数据，我应该先确认哪些风险点？",
          "把小红书和抖音的发布流程拆成可自动化的步骤。"
        ]
      }
    default:
      return {
        greeting: "说清你的目标，我会先帮你拆成可执行步骤",
        summary: "适合梳理需求、生成方案、组织信息和推进执行",
        prompts: [
          "你好啊",
          "请做一个自我介绍，给出一个你的能力清单"
        ]
      }
  }
}

export function getExpertGuideContent(project: ExpertProject | undefined): ExpertGuideContent {
  const expertKey = resolveExpertKey(project)
  if (expertKey !== "douyin") {
    return getExpertGuide(project)
  }

  return {
    greeting: "先给我一个视频目标，我来生成文案和分镜预览。",
    summary: "",
    intro: "补齐这些信息后，我会先出预览，不会直接开跑视频。",
    requirementChecklist: [
      "主题",
      "人群",
      "目标",
      "风格",
      "时长",
      "视频类型",
      "素材"
    ],
    routeOptions: [
      {
        title: "数字人口播",
        detail: "适合讲解、产品说明和知识类内容。继续前确认人物图片和配音方式。",
        accent: "host"
      },
      {
        title: "纯画面展示",
        detail: "适合氛围片、空镜、场景展示和旁白视频。先确认秒数和画面风格。",
        accent: "visual"
      }
    ],
    workflowSteps: [
      "补全 brief",
      "生成预览",
      "确认后开跑视频"
    ],
    // continueHint: "看完预览后，你可以直接回复：继续生成；如果要改，直接回复：修改第几镜 + 你的修改意见。",
    placeholder: "例如：做一个讲天气预报的抖音视频，给职场人看，目标是科普，男声数字人口播，10 秒，氛围感，我会上传人物照片，先给我文案和分镜预览。",
    prompts: [
      "做一个海边视频不要出现人物，给附近上班族看，目标是到店引流，纯画面展示，15 秒，真实手机拍摄感",
      "做一个护肤品种草短视频，给 25-35 岁女性看，目标是种草转化，纯画面加旁白，10 秒，精致广告感"
    ]
  }
}
