/**
 * Tests for the arrival Online-Board route page.
 *
 * Verifies that out-of-window dates redirect to /{locale}/onlineboard (TZ §4.1.2),
 * in-window dates render normally, and malformed URLs still produce 404.
 *
 * @vitest-environment jsdom
 */

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { render, screen } from "@testing-library/react";
import React from "react";

// Freeze clock: today = 2026-05-15. Board window: 2026-05-14 … 2026-05-29.
beforeEach(() => {
  vi.useFakeTimers();
  vi.setSystemTime(new Date(2026, 4, 15, 12));
});
afterEach(() => {
  vi.useRealTimers();
});

// --- mocks ---

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

vi.mock("@/env/index.js", () => ({
  getEnv: () => ({ PROD_ORIGIN: "https://aeroflot.ru" }),
}));

vi.mock("@/ui/seo/SeoHead.js", () => ({
  SeoHead: () => <div data-testid="seo-head" />,
}));

vi.mock("@/ui/errors/ErrorPage.js", () => ({
  ErrorPage: ({ code }: { code: string }) => <div data-testid={`error-${code}`} />,
}));

vi.mock("@/ui/flights/FlightListSkeleton.js", () => ({
  FlightListSkeleton: () => <div data-testid="skeleton" />,
}));

vi.mock(
  "@/features/online-board/components/OnlineBoardSearchPage.js",
  () => ({ OnlineBoardSearchPage: () => <div data-testid="board-search" /> }),
);

vi.mock("@/features/online-board/seo.js", () => ({
  buildArrivalSearchSeo: () => ({}),
}));

vi.mock("@/shared/hooks/useDictionaries.js", () => ({
  useCityName: (code: string) => code,
}));

const mockNavigate = vi.fn();
vi.mock("@modern-js/runtime/router", () => ({
  useParams: vi.fn(),
  Navigate: ({ to }: { to: string }) => {
    mockNavigate(to);
    return <div data-testid="navigate" data-to={to} />;
  },
}));

import { useParams } from "@modern-js/runtime/router";
import ArrivalSearchPage from "./page.js";

function renderWithParams(params: string, lang = "ru-ru") {
  vi.mocked(useParams).mockReturnValue({ params, lang });
  return render(<ArrivalSearchPage />);
}

describe("ArrivalSearchPage — date-window guard", () => {
  beforeEach(() => {
    mockNavigate.mockReset();
  });

  it("renders normally for an in-window date (today)", () => {
    renderWithParams("SVO-20260515");
    expect(screen.getByTestId("seo-head")).toBeTruthy();
    expect(screen.queryByTestId("navigate")).toBeNull();
  });

  it("renders normally for the earliest allowed date (today - 1)", () => {
    renderWithParams("SVO-20260514");
    expect(screen.queryByTestId("navigate")).toBeNull();
  });

  it("redirects when date is too late (today + 15)", () => {
    renderWithParams("SVO-20260530");
    expect(screen.getByTestId("navigate")).toBeTruthy();
    expect(mockNavigate).toHaveBeenCalledWith("/ru-ru/onlineboard");
  });

  it("redirects when date is too early (today - 2)", () => {
    renderWithParams("SVO-20260513");
    expect(screen.getByTestId("navigate")).toBeTruthy();
    expect(mockNavigate).toHaveBeenCalledWith("/ru-ru/onlineboard");
  });

  it("uses the route locale in the redirect path", () => {
    renderWithParams("SVO-20260530", "en-us");
    expect(mockNavigate).toHaveBeenCalledWith("/en-us/onlineboard");
  });

  it("shows 404 for a malformed URL (no date)", () => {
    renderWithParams("SVO");
    expect(screen.getByTestId("error-404")).toBeTruthy();
    expect(screen.queryByTestId("navigate")).toBeNull();
  });
});
