/**
 * Tests for OnlineBoardSearchPage component.
 *
 * Verifies rendering with mock providers and navigation wiring.
 *
 * @vitest-environment jsdom
 */

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen } from "@testing-library/react";
import { OnlineBoardSearchPage } from "./OnlineBoardSearchPage.js";
import type { OnlineBoardSearchPageProps } from "./OnlineBoardSearchPage.js";

// Mock i18n
vi.mock("@/i18n/provider.js", () => ({
  useTranslation: () => ({
    t: (key: string) => key,
    i18n: { language: "ru" },
  }),
}));

// Mock all hooks and router
vi.mock("@modern-js/runtime/router", () => ({
  useNavigate: () => vi.fn(),
  useParams: () => ({ lang: "ru" }),
  Link: ({ children, ...props }: Record<string, unknown>) =>
    <a {...props}>{children as React.ReactNode}</a>,
}));

// Mock layout components that bring in extra transitive dependencies
vi.mock("@/ui/layout/PageTabs.js", () => ({
  PageTabs: () => <div data-testid="page-tabs" />,
}));

vi.mock("./OnlineBoardFilter.js", () => ({
  OnlineBoardFilter: () => <div data-testid="online-board-filter" />,
}));

vi.mock("@/shared/hooks/useSearchHistory.js", () => ({
  useSearchHistory: () => ({ items: [], add: vi.fn(), clear: vi.fn() }),
}));

vi.mock("@/features/flights-map/hooks/useFeatureFlag.js", () => ({
  useFeatureFlag: () => false,
}));

vi.mock("../hooks/useOnlineBoard.js", () => ({
  useOnlineBoard: () => ({
    flights: [],
    loading: false,
    error: null,
    refresh: vi.fn(),
  }),
}));

vi.mock("../hooks/useLiveBoardSearch.js", () => ({
  useLiveBoardSearch: (_params: unknown, initialFlights: unknown) => ({
    flights: initialFlights,
    connectionStatus: "idle" as const,
  }),
}));

vi.mock("../hooks/useCalendarDays.js", () => ({
  useCalendarDays: () => ({
    days: [],
    loading: false,
  }),
}));

describe("OnlineBoardSearchPage", () => {
  const departureParsedParams: OnlineBoardSearchPageProps["params"] = {
    type: "departure",
    station: "SVO",
    date: "20250115",
  };

  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("renders search page container", () => {
    render(<OnlineBoardSearchPage params={departureParsedParams} />);
    expect(screen.getByTestId("online-board-search")).toBeTruthy();
  });

  it("renders empty flight list when no results", () => {
    render(<OnlineBoardSearchPage params={departureParsedParams} />);
    expect(screen.getByText("No flights found")).toBeTruthy();
  });

  it("renders for flight search type", () => {
    render(
      <OnlineBoardSearchPage
        params={{
          type: "flight",
          carrier: "SU",
          flightNumber: "100",
          date: "20250115",
        }}
      />,
    );
    expect(screen.getByTestId("online-board-search")).toBeTruthy();
  });

  it("renders for route search type", () => {
    render(
      <OnlineBoardSearchPage
        params={{
          type: "route",
          departure: "SVO",
          arrival: "JFK",
          date: "20250115",
        }}
      />,
    );
    expect(screen.getByTestId("online-board-search")).toBeTruthy();
  });

  it("renders for arrival search type", () => {
    render(
      <OnlineBoardSearchPage
        params={{
          type: "arrival",
          station: "JFK",
          date: "20250115",
        }}
      />,
    );
    expect(screen.getByTestId("online-board-search")).toBeTruthy();
  });
});
