// @vitest-environment happy-dom
import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanup, render, fireEvent } from "@testing-library/react";
import { BulkBar } from "./BulkBar";

afterEach(() => cleanup());

describe("BulkBar", () => {
  it("renders action buttons appropriate for the lane", () => {
    const { container } = render(
      <BulkBar
        lane="rejected"
        count={3}
        onResubmit={vi.fn()}
        onAcknowledge={() => {}}
        onDismiss={vi.fn()}
        onExport={vi.fn()}
      />,
    );
    expect(container.textContent).toMatch(/re-?submit/i);
    expect(container.textContent).toMatch(/export/i);
    expect(container.textContent).not.toMatch(/dismiss/i);
  });

  it("calls onResubmit when Resubmit clicked", () => {
    const onResubmit = vi.fn();
    const { container } = render(
      <BulkBar
        lane="rejected"
        count={2}
        onResubmit={onResubmit}
        onAcknowledge={() => {}}
        onDismiss={() => {}}
        onExport={() => {}}
      />,
    );
    const buttons = container.querySelectorAll("button");
    const resubmitBtn = Array.from(buttons).find((b) => /re-?submit/i.test(b.textContent || ""));
    expect(resubmitBtn).toBeDefined();
    fireEvent.click(resubmitBtn!);
    expect(onResubmit).toHaveBeenCalledOnce();
  });

  it("SP14: payer_rejected lane shows Acknowledge (not Resubmit/Dismiss)", () => {
    // Payer-rejected claims are not eligible for resubmit (the payer
    // already said no); the operator can only acknowledge them as
    // "I've seen this" and export the row set for follow-up.
    const onAcknowledge = vi.fn();
    const { container, getByTestId } = render(
      <BulkBar
        lane="payer_rejected"
        count={4}
        onResubmit={() => {}}
        onAcknowledge={onAcknowledge}
        onDismiss={() => {}}
        onExport={() => {}}
      />,
    );
    expect(container.textContent).toMatch(/acknowledge/i);
    expect(container.textContent).not.toMatch(/re-?submit/i);
    expect(container.textContent).not.toMatch(/dismiss/i);
    const ack = getByTestId("payer-rejected-acknowledge");
    fireEvent.click(ack);
    expect(onAcknowledge).toHaveBeenCalledOnce();
  });

  it("hides when count is zero", () => {
    const { container } = render(
      <BulkBar
        lane="rejected"
        count={0}
        onResubmit={() => {}}
        onAcknowledge={() => {}}
        onDismiss={() => {}}
        onExport={() => {}}
      />,
    );
    expect(container.firstChild).toBeNull();
  });
});
