// @vitest-environment happy-dom
import { afterEach, describe, expect, it } from "vitest";
import { cleanup, render } from "@testing-library/react";
import { LineReconciliationTab } from "./LineReconciliationTab";
import type { LineReconciliationResponse } from "@/types";

afterEach(() => cleanup());

const matchedAll: LineReconciliationResponse = {
  claimId: "CLM-1",
  summary: {
    billedTotal: "200.00",
    paidTotal: "160.00",
    adjustmentTotal: "40.00",
    matchedLines: 2,
    totalLines: 2,
  },
  lines: [
    {
      claimServiceLine: {
        lineNumber: 1,
        procedureQualifier: "HC",
        procedureCode: "99213",
        modifiers: [],
        charge: "100.00",
        units: "1",
        unitType: "UN",
        serviceDate: "2026-06-01",
      },
      serviceLinePayment: {
        id: 1,
        lineNumber: 1,
        procedureQualifier: "HC",
        procedureCode: "99213",
        modifiers: [],
        charge: "100.00",
        payment: "80.00",
        units: "1",
        unitType: "UN",
        serviceDate: "2026-06-01",
      },
      status: "matched",
      adjustments: [
        { groupCode: "CO", reasonCode: "45", amount: "20.00" },
      ],
    },
  ],
};

describe("LineReconciliationTab", () => {
  it("renders summary totals", () => {
    const { container } = render(<LineReconciliationTab data={matchedAll} />);
    expect(container.textContent).toMatch(/billed/i);
    expect(container.textContent).toMatch(/paid/i);
    expect(container.textContent).toMatch(/adjustments/i);
  });

  it("renders full-match summary line", () => {
    const { container } = render(<LineReconciliationTab data={matchedAll} />);
    expect(container.textContent).toMatch(/matched 2 of 2 lines/i);
  });

  it("renders partial-match summary line when lines are unmatched", () => {
    const partial: LineReconciliationResponse = {
      ...matchedAll,
      summary: { ...matchedAll.summary, matchedLines: 1, totalLines: 2 },
      lines: [
        matchedAll.lines[0]!,
        {
          claimServiceLine: {
            lineNumber: 2,
            procedureQualifier: "HC",
            procedureCode: "99214",
            modifiers: [],
            charge: "100.00",
            units: "1",
            unitType: "UN",
            serviceDate: "2026-06-01",
          },
          serviceLinePayment: null,
          status: "unmatched_837_only",
          adjustments: [],
        },
      ],
    };
    const { container } = render(<LineReconciliationTab data={partial} />);
    expect(container.textContent).toMatch(/matched 1 of 2 lines/i);
  });

  it("renders unmatched 835 line with oxblood accent", () => {
    const with835Only: LineReconciliationResponse = {
      ...matchedAll,
      lines: [
        matchedAll.lines[0]!,
        {
          claimServiceLine: null,
          serviceLinePayment: {
            id: 2,
            lineNumber: 2,
            procedureQualifier: "HC",
            procedureCode: "90837",
            modifiers: [],
            charge: "100.00",
            payment: "70.00",
            units: "1",
            unitType: "UN",
            serviceDate: "2026-06-01",
          },
          status: "unmatched_835_only",
          adjustments: [],
        },
      ],
    };
    const { container } = render(<LineReconciliationTab data={with835Only} />);
    expect(container.textContent).toMatch(/90837/);
    expect(container.textContent).toMatch(/no 837 line matched/i);
  });
});
