// @vitest-environment jsdom

import { cleanup, render, screen } from "@testing-library/react";
import type { ComponentProps } from "react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { NetworkAdminShell } from "@/components/network-admin-shell";
import { networkAdminShellStyles } from "@/components/network-admin-shell-styles";
import type { BallboxStoreClubDetail } from "@/lib/ballbox-store";

const statsSpy = vi.fn();
const createClubSectionSpy = vi.fn();
const clubListSpy = vi.fn();

vi.mock("@/components/network-admin-stats", () => ({
  NetworkAdminStats: (props: unknown) => {
    statsSpy(props);
    return <div data-testid="network-admin-stats" />;
  },
}));

vi.mock("@/components/network-admin-create-club-section", () => ({
  NetworkAdminCreateClubSection: (props: unknown) => {
    createClubSectionSpy(props);
    return <div data-testid="network-admin-create-club-section" />;
  },
}));

vi.mock("@/components/network-admin-club-list", () => ({
  NetworkAdminClubList: (props: unknown) => {
    clubListSpy(props);
    return <div data-testid="network-admin-club-list" />;
  },
}));

const clubs: BallboxStoreClubDetail[] = [
  {
    id: "club_1",
    atcSportclubId: "106",
    name: "Club One",
    locationName: null,
    venues: [],
  },
];

function renderShell(overrides?: Partial<ComponentProps<typeof NetworkAdminShell>>) {
  return render(
    <NetworkAdminShell
      clubs={clubs}
      setClubs={vi.fn()}
      busy={false}
      message="Saved"
      messageTone="success"
      runMutation={vi.fn()}
      clubForm={{ atcSportclubId: "106" }}
      setClubForm={vi.fn()}
      venueForms={{}}
      setVenueForms={vi.fn()}
      machineForms={{}}
      setMachineForms={vi.fn()}
      {...overrides}
    />,
  );
}

describe("NetworkAdminShell", () => {
  afterEach(() => {
    cleanup();
    statsSpy.mockReset();
    createClubSectionSpy.mockReset();
    clubListSpy.mockReset();
  });

  it("owns the layout classes and forwards the wired child props", () => {
    const setClubs = vi.fn();
    const runMutation = vi.fn();
    const setClubForm = vi.fn();
    const setVenueForms = vi.fn();
    const setMachineForms = vi.fn();

    renderShell({
      setClubs,
      runMutation,
      setClubForm,
      setVenueForms,
      setMachineForms,
      message: null,
      messageTone: "error",
      clubForm: { atcSportclubId: "107" },
      venueForms: { club_1: { name: "Venue One", slug: "venue-one", status: "inactive" } },
      machineForms: { venue_1: { label: "Machine One", active: false } },
    });

    expect(screen.getByTestId("network-admin-stats")).toBeTruthy();
    expect(screen.getByTestId("network-admin-create-club-section")).toBeTruthy();
    expect(screen.getByTestId("network-admin-club-list")).toBeTruthy();

    expect(statsSpy).toHaveBeenCalledWith(
      expect.objectContaining({
        clubs,
        cardClassName: networkAdminShellStyles.statCard,
      }),
    );

    expect(createClubSectionSpy).toHaveBeenCalledWith(
      expect.objectContaining({
        busy: false,
        message: null,
        messageTone: "error",
        clubForm: { atcSportclubId: "107" },
        setClubForm,
        emptyClubForm: { atcSportclubId: "" },
        cardClassName: networkAdminShellStyles.sectionCard,
        runMutation,
      }),
    );

    expect(clubListSpy).toHaveBeenCalledWith(
      expect.objectContaining({
        clubs,
        busy: false,
        setClubs,
        venueForms: { club_1: { name: "Venue One", slug: "venue-one", status: "inactive" } },
        setVenueForms,
        emptyVenueForm: { name: "", slug: "", status: "active" },
        machineForms: { venue_1: { label: "Machine One", active: false } },
        setMachineForms,
        emptyMachineForm: { label: "", active: true },
        venueStatusOptions: ["active", "inactive"],
        selectClassName: networkAdminShellStyles.select,
        articleCardClassName: networkAdminShellStyles.articleCard,
        mutedPanelClassName: networkAdminShellStyles.mutedPanel,
        insetPanelClassName: networkAdminShellStyles.insetPanel,
        wellClassName: networkAdminShellStyles.well,
        runMutation,
      }),
    );
  });
});
