import { describe, expect, test } from "bun:test";
import {
  type CalculatedDeadline,
  deadlineCardHtml,
} from "./verfahrensablauf-core";

// Regression tests for the editable→click-to-edit wiring on timeline date
// cells (m/paliad#59). When CardOpts.editable=true the card renderer must
// emit `class="… frist-date-edit"` with `data-rule-code` + `data-current-
// date` on the date span. Pages then attach a delegated click handler that
// resolves that selector to swap in an inline `<input type="date">`. If a
// future refactor drops the attrs, /tools/verfahrensablauf and
// /tools/fristenrechner both silently lose click-to-edit (no script error,
// nothing happens on click). These tests pin the contract.
//
// Fixture leaves ruleRef/legalSource* empty so deadlineCardHtml stays
// inside its non-DOM code paths (escHtml is DOM-backed and bun test runs
// in plain Node without jsdom).

const dl = (overrides: Partial<CalculatedDeadline> = {}): CalculatedDeadline => ({
  code: "upc-rop-12",
  name: "Klageerwiderung",
  nameEN: "Statement of Defence",
  party: "defendant",
  priority: "mandatory",
  ruleRef: "",
  dueDate: "2026-07-15",
  originalDate: "2026-07-15",
  wasAdjusted: false,
  isRootEvent: false,
  isCourtSet: false,
  ...overrides,
});

describe("deadlineCardHtml — editable=true emits click-to-edit attrs", () => {
  test("date span carries frist-date-edit class + data-rule-code + data-current-date", () => {
    const html = deadlineCardHtml(dl(), { showParty: true, editable: true });
    expect(html).toContain('class="timeline-date frist-date-edit"');
    expect(html).toContain('data-rule-code="upc-rop-12"');
    expect(html).toContain('data-current-date="2026-07-15"');
    expect(html).toContain('role="button"');
    expect(html).toContain('tabindex="0"');
  });

  test("editable=false (default) emits the date span without click-to-edit attrs", () => {
    const html = deadlineCardHtml(dl(), { showParty: true });
    expect(html).toContain("timeline-date");
    expect(html).not.toContain("data-rule-code=");
    expect(html).not.toContain('role="button"');
  });

  test("root event suppresses editable even when editable=true (root has no override semantic)", () => {
    const html = deadlineCardHtml(dl({ isRootEvent: true }), { showParty: true, editable: true });
    expect(html).not.toContain("data-rule-code=");
  });

  test("isCourtSet renders the court-set placeholder with click-to-edit so users can pin a real date", () => {
    const html = deadlineCardHtml(dl({ isCourtSet: true }), { showParty: true, editable: true });
    expect(html).toContain("timeline-court-set frist-date-edit");
    expect(html).toContain('data-rule-code="upc-rop-12"');
  });

  test("empty rule code with editable=true still suppresses click-to-edit (no anchor target)", () => {
    const html = deadlineCardHtml(dl({ code: "" }), { showParty: true, editable: true });
    expect(html).not.toContain("data-rule-code=");
  });
});
