import { describe, expect, it, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { FluentProvider, webLightTheme } from "@fluentui/react-components";
import { MemoryRouter } from "react-router-dom";
import { JailsPage } from "../JailsPage";
import type { JailSummary } from "../../types/jail";

const mockNavigate = vi.fn();

vi.mock("react-router-dom", async () => {
  const actual = (await vi.importActual<typeof import("react-router-dom")>(
    "react-router-dom",
  )) as unknown as Record<string, unknown>;
  return {
    ...actual,
    useNavigate: () => mockNavigate,
  };
});

vi.mock("../hooks/useJails", () => ({
  useJails: () => ({
    jails: [
      {
        name: "sshd",
        enabled: true,
        running: true,
        idle: false,
        backend: "systemd",
        find_time: 600,
        ban_time: 3600,
        max_retry: 5,
        status: {
          currently_banned: 1,
          total_banned: 10,
          currently_failed: 0,
          total_failed: 0,
        },
      },
    ] as JailSummary[],
    total: 1,
    loading: false,
    error: null,
    refresh: vi.fn(),
    startJail: vi.fn().mockResolvedValue(undefined),
    stopJail: vi.fn().mockResolvedValue(undefined),
    setIdle: vi.fn().mockResolvedValue(undefined),
    reloadJail: vi.fn().mockResolvedValue(undefined),
    reloadAll: vi.fn().mockResolvedValue(undefined),
  }),
}));

function renderPage() {
  return render(
    <MemoryRouter>
      <FluentProvider theme={webLightTheme}>
        <JailsPage />
      </FluentProvider>
    </MemoryRouter>,
  );
}

describe("JailsPage", () => {
  it("navigates to Configuration → Jails when a jail is clicked", async () => {
    renderPage();
    const user = userEvent.setup();

    await user.click(screen.getByText("sshd"));

    expect(mockNavigate).toHaveBeenCalledWith("/config", {
      state: { tab: "jails", jail: "sshd" },
    });
  });
});
