import React from "react";
import "@testing-library/jest-dom";
import { fireEvent, render, screen } from "@testing-library/react";
import { CssVarsProvider } from "@mui/material/styles";

import { I18nProvider } from "./i18n/I18nProvider";
import AppShell from "./layout/AppShell";
import { getTheme } from "./theme";

test("notification bell exposes its unread count and keyboard-accessible action", () => {
  const open = jest.fn();
  render(
    <CssVarsProvider theme={getTheme("light") as any} defaultMode="light">
      <I18nProvider>
        <AppShell
          pageTitle="Dashboard"
          breadcrumbs={["Home"]}
          pathname="/dashboard"
          nav={[]}
          navBottom={[]}
          onNavigate={() => undefined}
          onToggleDrawer={() => undefined}
          drawerOpen={false}
          notificationsCount={3}
          onOpenNotifications={open}
        >
          <div>Content</div>
        </AppShell>
      </I18nProvider>
    </CssVarsProvider>,
  );

  const bell = screen.getByRole("button", { name: "Notifications" });
  expect(screen.getByText("3")).toBeInTheDocument();
  fireEvent.click(bell);
  expect(open).toHaveBeenCalledTimes(1);
  expect(open.mock.calls[0][0]).toBeInstanceOf(HTMLElement);
});

test("shows build metadata only when the caller supplies the admin-only badge", () => {
  const commonProps = {
    pageTitle: "Dashboard",
    breadcrumbs: ["Home"],
    pathname: "/dashboard",
    nav: [],
    navBottom: [],
    onNavigate: () => undefined,
    onToggleDrawer: () => undefined,
    drawerOpen: false,
  };

  const { rerender } = render(
    <CssVarsProvider theme={getTheme("light") as any} defaultMode="light">
      <I18nProvider>
        <AppShell {...commonProps} buildMetadata={{ version: "2.4.1", commitSha: "abc1234" }}>
          <div>Content</div>
        </AppShell>
      </I18nProvider>
    </CssVarsProvider>,
  );

  expect(screen.getByTestId("admin-version-badge")).toHaveTextContent("v2.4.1");
  expect(screen.getByLabelText("Application version 2.4.1, commit abc1234")).toBeInTheDocument();

  rerender(
    <CssVarsProvider theme={getTheme("light") as any} defaultMode="light">
      <I18nProvider>
        <AppShell {...commonProps}>
          <div>Content</div>
        </AppShell>
      </I18nProvider>
    </CssVarsProvider>,
  );

  expect(screen.queryByTestId("admin-version-badge")).not.toBeInTheDocument();
});

test("shell icon controls expose accessible names", () => {
  render(
    <CssVarsProvider theme={getTheme("light") as any} defaultMode="light">
      <I18nProvider>
        <AppShell
          pageTitle="Dashboard"
          breadcrumbs={["Home"]}
          pathname="/dashboard"
          nav={[]}
          navBottom={[]}
          onNavigate={() => undefined}
          onToggleDrawer={() => undefined}
          drawerOpen={false}
          onOpenSettings={() => undefined}
          user={{ userName: "Ada", roleLabel: "Administrator" }}
        >
          <div>Content</div>
        </AppShell>
      </I18nProvider>
    </CssVarsProvider>,
  );

  expect(screen.getByRole("button", { name: "Collapse sidebar" })).toBeInTheDocument();
  expect(screen.getByRole("button", { name: "Notifications" })).toBeInTheDocument();
  expect(screen.getByRole("button", { name: "Settings" })).toBeInTheDocument();
  expect(screen.getByRole("button", { name: "User" })).toBeInTheDocument();
});
