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

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

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: [
          "帮我做一篇推荐平价火锅的笔记",
          "做一篇制作抹茶奶酪欧包教程的爆文。",
          "给通勤女生做一篇春季穿搭笔记。"
        ]
      }
    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: [
      "主题：产品、话题、门店活动或要讲的内容",
      "给谁看：目标人群、行业或使用场景",
      "目标：卖货、种草、科普、活动引流",
      "风格：精致广告感、真实手机拍摄感、氛围感、剧情感",
      "时长：数字人口播尽量控制在 65 秒内，纯画面请直接给秒数",
      "视频类型：有人出镜讲解，还是纯画面展示",
      "素材：有没有人物图片、现成音频，或准备走系统配音"
    ],
    routeOptions: [
      {
        title: " 有人出镜讲解",
        detail: "适合数字人口播、知识讲解、产品说明。继续生成前通常要确认人物图片，以及现成音频或男声/女声配音。",
        accent: "host"
      },
      {
        title: " 纯画面展示",
        detail: "适合氛围片、空镜、场景展示、画面加旁白。先把视频秒数说清楚，再出文案和分镜预览。",
        accent: "visual"
      }
    ],
    workflowSteps: [
      " 先生成文案和分镜预览，不直接开跑视频。",
      " 你确认预览后，再继续生成数字人或纯画面视频。",
      " 如果当前路线缺素材，再按需要补图片、音频或配音要求。"
    ],
    // continueHint: "看完预览后，你可以直接回复：继续生成；如果要改，直接回复：修改第几镜 + 你的修改意见。",
    placeholder: "例如：做一个讲天气预报的抖音视频，给职场人看，目标是科普，男声数字人口播，10 秒，氛围感，我会上传人物照片，先给我文案和分镜预览。",
    prompts: [
      "做一个海边视频不要出现人物，给附近上班族看，目标是到店引流，纯画面展示，15 秒，真实手机拍摄感",
      "做一个护肤品种草短视频，给 25-35 岁女性看，目标是种草转化，纯画面加旁白，10 秒，精致广告感"
    ]
  }
}
