// @vitest-environment happy-dom
import { describe, expect, it, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { InboxRow } from "./InboxRow";
import type { InboxClaimRow, InboxCandidateRow } from "@/lib/inbox-api";

const baseClaim: InboxClaimRow = {
  id: "CLP-7741",
  kind: "claim",
  patient_control_number: "PCN-001",
  charge_amount: 1240,
  payer_id: "CO-MCD",
  provider_npi: "1234567890",
  state: "rejected",
  rejection_reason: "999 AK9 R",
  rejected_at: "2026-06-20T09:12:00Z",
  service_date_from: null,
  score: null,
  score_tier: null,
  score_breakdown: null,
};

describe("InboxRow", () => {
  it("renders the claim id, payer, and formatted charge", () => {
    const { container } = render(
      <table>
        <tbody>
          <InboxRow row={baseClaim} accent="oxblood" onClick={() => {}} />
        </tbody>
      </table>,
    );
    expect(container.textContent).toContain("CLP-7741");
    expect(container.textContent).toContain("CO-MCD");
    expect(container.textContent).toContain("$1,240.00");
  });

  it("renders the rejection reason when present", () => {
    const { container } = render(
      <table>
        <tbody>
          <InboxRow row={baseClaim} accent="oxblood" onClick={() => {}} />
        </tbody>
      </table>,
    );
    expect(container.textContent).toContain("999 AK9 R");
  });

  it("calls onClick when the row is clicked", () => {
    const onClick = vi.fn();
    const { container } = render(
      <table>
        <tbody>
          <InboxRow row={baseClaim} accent="oxblood" onClick={onClick} />
        </tbody>
      </table>,
    );
    const row = container.querySelector("tr");
    expect(row).not.toBeNull();
    fireEvent.click(row!);
    expect(onClick).toHaveBeenCalledOnce();
  });

  it("renders the top candidate score for remit rows", () => {
    const candidateRow: InboxCandidateRow = {
      id: "R-1",
      kind: "remit",
      payer_claim_control_number: "PCN-X",
      charge_amount: 500,
      payer_id: "AETNA",
      rendering_provider_npi: "1234567890",
      service_date: null,
      candidates: [
        {
          claim_id: "C-1",
          score: 92,
          tier: "strong",
          breakdown: { patient: 1, date: 1, amount: 1, provider: 1 },
        },
      ],
    };
    const { container } = render(
      <table>
        <tbody>
          <InboxRow row={candidateRow} accent="amber" onClick={() => {}} />
        </tbody>
      </table>,
    );
    expect(container.textContent).toContain("92");
  });
});
