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

import {
  buildCandidateIndex,
  buildPatternRows,
  candidateKeysForWorkplan,
  formatActionCount,
  normalizeTriageReports,
  resolveCandidate,
  topAction,
  truncateSummary,
} from "../src/components/wsjf-triage.js";

test("candidate keys resolve workplan ids and filename slugs", () => {
  assert.deepEqual(
    candidateKeysForWorkplan({filename: "CUST-WP-0003-whi-kpi-card.md"}).sort(),
    ["cust-wp-0003", "cust-wp-0003-whi-kpi-card", "whi-kpi-card"],
  );
  assert.deepEqual(
    candidateKeysForWorkplan({filename: "260501-CUST-WP-0028-e2e-sandbox-framework.md"}).sort(),
    ["cust-wp-0028", "cust-wp-0028-e2e-sandbox-framework", "e2e-sandbox-framework"],
  );
});

test("candidate index resolves daily report candidates to workstream ids", () => {
  const index = buildCandidateIndex({
    workstreams: {
      "d9d9a3ec-f736-4041-beac-bb92c7ad314e": {
        filename: "CUST-WP-0045-activity-core-daily-triage-runner.md",
      },
      "9cc32158-2f5c-4ef6-9713-aacce4623d5e": {
        filename: "CUST-WP-0003-whi-kpi-card.md",
      },
      "36162ff0-9b47-47c4-8602-56767f9b7a1c": {
        filename: "ADHOC-2026-06-01.md",
      },
    },
  });

  assert.equal(resolveCandidate("cust-wp-0045", index).id, "d9d9a3ec-f736-4041-beac-bb92c7ad314e");
  assert.equal(resolveCandidate("whi-kpi-card", index).id, "9cc32158-2f5c-4ef6-9713-aacce4623d5e");
  assert.equal(resolveCandidate("adhoc-2026-06-01", index).id, "36162ff0-9b47-47c4-8602-56767f9b7a1c");
  assert.equal(resolveCandidate("missing-wp-0001", index), null);
});

test("triage events normalize reports and action summaries", () => {
  const [report] = normalizeTriageReports([
    {
      id: "935244fa-b438-488c-a11a-42e1a84e3d59",
      summary: "event summary",
      created_at: "2026-06-02T12:52:14.460214Z",
      author: "activity-core",
      detail: {
        scheduled_for: "2026-06-02T12:52:01.690214+00:00",
        instruction_id: "daily-triage-report",
        activity_core_run_id: "f9b97749-c1d0-5746-ab18-89932bef47c1",
        report: {
          summary: "daily summary",
          recommendations: [
            {candidate: "cust-wp-0045", action: "work-next", confidence: "high", why: "runner"},
            {candidate: "cust-wp-0046", action: "needs-human", confidence: "high", why: "blocked"},
            {candidate: "whi-kpi-card", action: "work-next", confidence: "medium", why: "health"},
          ],
        },
      },
    },
  ]);

  assert.equal(report.summary, "daily summary");
  assert.equal(report.date, "2026-06-02");
  assert.equal(report.recommendations.length, 3);
  assert.equal(report.memory_path, "the-custodian/memory/working/daily-triage-2026-06-02-f9b97749.md");
  assert.deepEqual(topAction(report.recommendations), {action: "work-next", count: 2});
  assert.equal(formatActionCount(topAction(report.recommendations)), "work-next x2");
});

test("pattern rows aggregate repeated candidates by action", () => {
  const reports = normalizeTriageReports([
    {
      id: "one",
      created_at: "2026-06-03T06:00:00Z",
      detail: {
        report: {
          recommendations: [
            {candidate: "cust-wp-0046", action: "needs-human"},
            {candidate: "cust-wp-0045", action: "work-next"},
          ],
        },
      },
    },
    {
      id: "two",
      created_at: "2026-06-02T06:00:00Z",
      detail: {
        report: {
          recommendations: [
            {candidate: "cust-wp-0046", action: "needs-human"},
            {candidate: "cust-wp-0046", action: "revisit"},
          ],
        },
      },
    },
  ]);

  const [first, second] = buildPatternRows(reports);
  assert.equal(first.candidateKey, "cust-wp-0046");
  assert.equal(first.count, 3);
  assert.equal(first.action, "needs-human");
  assert.equal(first.actionCount, 2);
  assert.equal(second.candidateKey, "cust-wp-0045");
});

test("summary truncation keeps compact table rows", () => {
  assert.equal(truncateSummary("short", 120), "short");
  assert.equal(truncateSummary("abcdefghijklmnopqrstuvwxyz", 10), "abcdefg...");
});
