import test from "node:test"
import assert from "node:assert/strict"

test("loads real task panel data from the desktop bridge when available", async () => {
  const calls: string[] = []
  globalThis.window = {
    qjcDesktop: {
      tasks: {
        listByDate: async (date: string) => {
          calls.push(date)
          return [
            {
              id: "real-task",
              date,
              expertName: "小红书专家",
              taskTitle: "真实任务",
              status: "completed",
              statusDetail: "已完成",
              messageCount: 2,
              updatedAt: "10:00:00",
              artifacts: []
            }
          ]
        }
      }
    }
  } as unknown as Window & typeof globalThis

  const { loadTaskPanelItems } = await import("../src/features/tasks/taskPanelData.ts")
  const items = await loadTaskPanelItems("2026-05-15")

  assert.deepEqual(calls, ["2026-05-15"])
  assert.equal(items[0]?.expertName, "小红书专家")
})

test("summarizes task panel items from real artifacts and messages", async () => {
  const { summarizeTaskPanelItems } = await import("../src/features/tasks/taskPanelData.ts")

  assert.deepEqual(summarizeTaskPanelItems([
    {
      id: "one",
      date: "2026-05-15",
      expertName: "小红书专家",
      taskTitle: "one",
      status: "completed",
      statusDetail: "已完成",
      messageCount: 2,
      updatedAt: "10:00:00",
      artifacts: [
        { id: "a", name: "a.md", path: "/tmp/a.md" },
        { id: "b", name: "b.png", path: "/tmp/b.png" }
      ]
    },
    {
      id: "two",
      date: "2026-05-15",
      expertName: "抖音专家",
      taskTitle: "two",
      status: "completed",
      statusDetail: "已完成",
      messageCount: 2,
      updatedAt: "11:00:00",
      artifacts: [
        { id: "c", name: "c.mp4", path: "/tmp/c.mp4" }
      ]
    }
  ]), {
    creditsUsed: 0,
    messageCount: 4,
    artifactCount: 3,
    employeeCount: 2
  })
})

test("filters task panel artifacts for Douyin experts only", async () => {
  const { getVisibleTaskPanelArtifacts } = await import("../src/features/tasks/taskPanelData.ts")

  const douyinArtifacts = [
    { id: "script", name: "口播脚本.docx", path: "/tmp/口播脚本.docx" },
    { id: "video", name: "成片.MP4", path: "/tmp/成片.MP4" },
    { id: "notes", name: "执行记录.md", path: "/tmp/执行记录.md" },
    { id: "sheet", name: "素材清单.xlsx", path: "/tmp/素材清单.xlsx" },
    { id: "cover", name: "封面.png", path: "/tmp/封面.png" }
  ]
  const xhsArtifacts = [
    { id: "xhs-note", name: "小红书笔记.md", path: "/tmp/小红书笔记.md" },
    { id: "xhs-cover", name: "封面.png", path: "/tmp/封面.png" }
  ]

  assert.deepEqual(
    getVisibleTaskPanelArtifacts({ expertName: "抖音专家", artifacts: douyinArtifacts }),
    [douyinArtifacts[0], douyinArtifacts[1]]
  )
  assert.deepEqual(
    getVisibleTaskPanelArtifacts({ expertName: "openclaw-douyin-skills-delivery", artifacts: douyinArtifacts }),
    [douyinArtifacts[0], douyinArtifacts[1]]
  )
  assert.deepEqual(
    getVisibleTaskPanelArtifacts({ expertName: "小红书专家", artifacts: xhsArtifacts }),
    xhsArtifacts
  )
})

test("summarizes artifact count from visible task panel artifacts", async () => {
  const { summarizeTaskPanelItems } = await import("../src/features/tasks/taskPanelData.ts")

  assert.deepEqual(summarizeTaskPanelItems([
    {
      id: "douyin",
      date: "2026-05-15",
      expertName: "抖音专家",
      taskTitle: "douyin",
      status: "completed",
      statusDetail: "已完成",
      creditsUsed: 8,
      messageCount: 3,
      updatedAt: "10:00:00",
      artifacts: [
        { id: "docx", name: "脚本.docx", path: "/tmp/脚本.docx" },
        { id: "mp4", name: "成片.mp4", path: "/tmp/成片.mp4" },
        { id: "md", name: "过程.md", path: "/tmp/过程.md" },
        { id: "png", name: "封面.png", path: "/tmp/封面.png" }
      ]
    },
    {
      id: "xhs",
      date: "2026-05-15",
      expertName: "小红书专家",
      taskTitle: "xhs",
      status: "completed",
      statusDetail: "已完成",
      creditsUsed: 5,
      messageCount: 2,
      updatedAt: "11:00:00",
      artifacts: [
        { id: "xhs-md", name: "笔记.md", path: "/tmp/笔记.md" },
        { id: "xhs-png", name: "封面.png", path: "/tmp/封面.png" }
      ]
    }
  ]), {
    creditsUsed: 13,
    messageCount: 5,
    artifactCount: 4,
    employeeCount: 2
  })
})
