/**
 * Online Board arrival search route.
 *
 * Parses station params from URL, renders shared search page with SEO.
 * URL: /{lang}/onlineboard/arrival/{station}-{yyyyMMdd}[-{timeRange}]
 */

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

const OnlineBoardSearchPage = lazy(() =>
  import("@/features/online-board/components/OnlineBoardSearchPage.js").then(
    (m) => ({ default: m.OnlineBoardSearchPage }),
  ),
);

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

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

  const canonicalOrigin = getEnv().PROD_ORIGIN;
  const searchParams = {
    type: "arrival" as const,
    station: parsed.station,
    date: parsed.date,
    ...(parsed.timeFrom !== undefined && parsed.timeTo !== undefined
      ? { timeFrom: parsed.timeFrom, timeTo: parsed.timeTo }
      : {}),
  };

  const seoProps = buildArrivalSearchSeo(t, searchParams, locale, canonicalOrigin);

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