/**
 * Tests for the round-trip schedule route search page.
 *
 * Verifies that out-of-window dates (outbound OR inbound) redirect to
 * /{locale}/schedule (TZ §4.1.2-R11), 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. Schedule window: 2026-05-14 … 2027-04-10.
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/schedule/components/ScheduleSearchPage.js",
  () => ({ ScheduleSearchPage: () => <div data-testid="schedule-search" /> }),
);

vi.mock("@/features/schedule/seo.js", () => ({
  buildScheduleSearchSeo: () => ({}),
}));

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 ScheduleRoundTripSearchPage from "./page.js";

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

// In-window segment: SVO-LED-20260515-20260515 (today, well within window)
// Out-of-window (future): SVO-LED-20270411-20270411 (today + 331)
// Out-of-window (past): SVO-LED-20260513-20260513 (today - 2)

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

  it("renders normally when both dates are in-window", () => {
    renderWithParams(
      "SVO-LED-20260515-20260515",
      "LED-SVO-20260520-20260520",
    );
    expect(screen.getByTestId("seo-head")).toBeTruthy();
    expect(screen.queryByTestId("navigate")).toBeNull();
  });

  it("redirects when outbound date is too late (> +330)", () => {
    renderWithParams(
      "SVO-LED-20270411-20270411",
      "LED-SVO-20270415-20270415",
    );
    expect(screen.getByTestId("navigate")).toBeTruthy();
    expect(mockNavigate).toHaveBeenCalledWith("/ru-ru/schedule");
  });

  it("redirects when outbound date is too early (< -1)", () => {
    renderWithParams(
      "SVO-LED-20260513-20260513",
      "LED-SVO-20260520-20260520",
    );
    expect(screen.getByTestId("navigate")).toBeTruthy();
    expect(mockNavigate).toHaveBeenCalledWith("/ru-ru/schedule");
  });

  it("redirects when inbound date is out-of-window while outbound is in-window", () => {
    renderWithParams(
      "SVO-LED-20260515-20260515",
      "LED-SVO-20270411-20270411",
    );
    expect(screen.getByTestId("navigate")).toBeTruthy();
    expect(mockNavigate).toHaveBeenCalledWith("/ru-ru/schedule");
  });

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

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

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