/**
 * Tests for the MainLayout component.
 *
 * Covers:
 * - BanGUI application version displayed in the footer when the sidebar is expanded.
 * - Version text hidden when the sidebar is collapsed.
 * - Navigation items rendered correctly.
 */

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MemoryRouter } from "react-router-dom";
import { FluentProvider, webLightTheme } from "@fluentui/react-components";
import { MainLayout } from "../../layouts/MainLayout";

// ---------------------------------------------------------------------------
// Mocks
// ---------------------------------------------------------------------------

vi.mock("../../hooks/useAuth", () => ({
  useAuth: () => ({ logout: vi.fn() }),
}));

vi.mock("../../hooks/useServerStatus", () => ({
  useServerStatus: () => ({
    status: { online: true, version: "1.0.0", active_jails: 1, total_bans: 0, total_failures: 0 },
    loading: false,
    error: null,
    refresh: vi.fn(),
  }),
}));

vi.mock("../../hooks/useBlocklistStatus", () => ({
  useBlocklistStatus: () => ({ hasErrors: false }),
}));

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

function renderLayout(): void {
  render(
    <FluentProvider theme={webLightTheme}>
      <MemoryRouter initialEntries={["/"]}>
        <MainLayout />
      </MemoryRouter>
    </FluentProvider>,
  );
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

describe("MainLayout", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    localStorage.clear();
  });

  it("renders the navigation sidebar", () => {
    renderLayout();
    expect(screen.getByRole("navigation", { name: "Main navigation" })).toBeInTheDocument();
  });

  it("does not show the BanGUI application version in the sidebar footer", () => {
    renderLayout();
    // __APP_VERSION__ is stubbed to "0.0.0-test" via vitest.config.ts define.
    expect(screen.queryByText(/BanGUI v/)).not.toBeInTheDocument();
  });

  it("hides the logo text when the sidebar is collapsed", async () => {
    renderLayout();
    const toggleButton = screen.getByRole("button", { name: /collapse sidebar/i });
    await userEvent.click(toggleButton);
    expect(screen.queryByText("BanGUI")).not.toBeInTheDocument();
  });

  it("initialises sidebar collapsed state from localStorage", () => {
    localStorage.setItem("bangui_sidebar_collapsed", "true");
    renderLayout();
    expect(screen.queryByText("BanGUI")).not.toBeInTheDocument();
  });

  it("persists sidebar collapsed state to localStorage when toggled", async () => {
    renderLayout();
    const toggleButton = screen.getByRole("button", { name: /collapse sidebar/i });
    await userEvent.click(toggleButton);
    expect(localStorage.getItem("bangui_sidebar_collapsed")).toBe("true");
  });

  it("renders a theme toggle button in the sidebar footer", () => {
    renderLayout();
    expect(screen.getByRole("button", { name: /switch to dark mode/i })).toBeInTheDocument();
  });
});
