import { render, screen, fireEvent } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { TabHeaders } from "./TabHeaders";

test("renders tab headers and handles switching", () => {
  const onTabChange = vi.fn();

  render(
    <TabHeaders
      activeTab="general"
      isLicensed={true}
      onTabChange={onTabChange}
    />,
  );

  expect(screen.getByText("General")).toBeInTheDocument();
  expect(screen.getByText("Rules")).toBeInTheDocument();
  expect(screen.getByText("License")).toBeInTheDocument();

  fireEvent.click(screen.getByText("Rules"));

  expect(onTabChange).toHaveBeenCalledWith("rules");
});

test("shows warning badge on License tab when unlicensed", () => {
  render(
    <TabHeaders
      activeTab="license"
      isLicensed={false}
      onTabChange={() => {}}
    />,
  );

  expect(screen.getByText("License ⚠️")).toBeInTheDocument();
});

test("General and Rules tabs are disabled when unlicensed", () => {
  render(
    <TabHeaders
      activeTab="license"
      isLicensed={false}
      onTabChange={() => {}}
    />,
  );

  expect(screen.getByRole("tab", { name: "General" })).toBeDisabled();
  expect(screen.getByRole("tab", { name: "Rules" })).toBeDisabled();
  expect(screen.getByRole("tab", { name: /License/i })).not.toBeDisabled();
});

test("clicking disabled tabs when unlicensed does not fire onTabChange", () => {
  const onTabChange = vi.fn();

  render(
    <TabHeaders
      activeTab="license"
      isLicensed={false}
      onTabChange={onTabChange}
    />,
  );

  fireEvent.click(screen.getByRole("tab", { name: "General" }));
  fireEvent.click(screen.getByRole("tab", { name: "Rules" }));

  expect(onTabChange).not.toHaveBeenCalled();
});
