/**
 * Schedule one-way route search page.
 *
 * Parses route direction params from URL, renders search page with SEO.
 * URL: /{lang}/schedule/route/{dep}-{arr}-{dateFrom}-{dateTo}[-{timeRange}][-C{conn}]
 */

import { lazy, Suspense } from "react";
import { useParams } from "@modern-js/runtime/router";
import { useTranslation } from "@/i18n/provider.js";
import { parseScheduleRouteParams } from "@/features/schedule/url.js";
import { buildScheduleSearchSeo } from "@/features/schedule/seo.js";
import { SeoHead } from "@/ui/seo/SeoHead.js";
import { FlightListSkeleton } from "@/ui/flights/FlightListSkeleton.js";
import { getEnv } from "@/env/index.js";

const ScheduleSearchPage = lazy(() =>
  import("@/features/schedule/components/ScheduleSearchPage.js").then(
    (m) => ({ default: m.ScheduleSearchPage }),
  ),
);

export default function ScheduleRouteSearchPage(): JSX.Element {
  const { t } = useTranslation();
  const routeParams = useParams<{ params: string; lang: string }>();
  const raw = routeParams.params ?? "";
  const locale = routeParams.lang ?? "ru";
  const parsed = parseScheduleRouteParams(raw);

  if (!parsed) {
    return (
      <div data-testid="invalid-params">
        <p>Invalid schedule route parameters.</p>
      </div>
    );
  }

  const canonicalOrigin = getEnv().PROD_ORIGIN;
  const scheduleParams = { type: "route" as const, outbound: parsed };
  const seoProps = buildScheduleSearchSeo(t, scheduleParams, locale, canonicalOrigin);

  return (
    <>
      <SeoHead {...seoProps} />
      <Suspense fallback={<FlightListSkeleton />}>
        <ScheduleSearchPage params={scheduleParams} />
      </Suspense>
    </>
  );
}
