/**
 * SEO builder functions for Schedule route pages.
 *
 * Each function is PURE -- all data arrives via parameters, no hooks or
 * side effects. Returns a SeoHeadProps object ready for <SeoHead>.
 *
 * Translation keys follow the pattern:
 * SEO.SCHEDULE.{MAIN,SEARCH,DETAILS}.{TITLE,DESCRIPTION}
 *
 * @module
 */

import type { SeoHeadProps } from "@/ui/seo/SeoHead.js";
import { buildHreflangSet } from "@/shared/seo/hreflang.js";
import { buildScheduleUrl } from "./url.js";
import type { ScheduleParams } from "./url.js";
import type { ISimpleFlight, IScheduleFlightId } from "./types.js";

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TFunction = (key: string, opts?: any) => string;

export interface CityNames {
  departure?: string;
  arrival?: string;
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

const OG_IMAGE = "https://www.aeroflot.ru/static/images/aeroflot-og-default.png";
const SITE_NAME = "Aeroflot";

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

function formatDateForSeo(yyyymmdd: string): string {
  if (yyyymmdd.length !== 8) return yyyymmdd;
  const day = yyyymmdd.slice(6, 8);
  const month = yyyymmdd.slice(4, 6);
  const year = yyyymmdd.slice(0, 4);
  return `${day}.${month}.${year}`;
}

function buildCanonical(
  canonicalOrigin: string,
  locale: string,
  params: ScheduleParams,
): string {
  const path = buildScheduleUrl(params);
  return `${canonicalOrigin}/${locale}/${path}`;
}

function buildPathWithoutLocale(params: ScheduleParams): string {
  const path = buildScheduleUrl(params);
  return `/${path}`;
}

function buildCommonSeoProps(args: {
  title: string;
  description: string;
  canonical: string;
  hreflangPath: string;
  canonicalOrigin: string;
  locale: string;
  ogType: "website" | "article";
}): Pick<SeoHeadProps, "og" | "twitter" | "hreflang"> {
  return {
    hreflang: buildHreflangSet({
      canonicalOrigin: args.canonicalOrigin,
      pathWithoutLocale: args.hreflangPath,
    }),
    og: {
      title: args.title,
      description: args.description,
      url: args.canonical,
      image: OG_IMAGE,
      type: args.ogType,
      locale: args.locale,
      siteName: SITE_NAME,
    },
    twitter: {
      card: "summary",
    },
  };
}

// ---------------------------------------------------------------------------
// Public SEO builder functions
// ---------------------------------------------------------------------------

/**
 * SEO props for the Schedule start page.
 */
export function buildScheduleStartSeo(
  t: TFunction,
  locale: string,
  canonicalOrigin: string,
): SeoHeadProps {
  const params: ScheduleParams = { type: "start" };
  const title = t("SEO.SCHEDULE.MAIN.TITLE");
  const description = t("SEO.SCHEDULE.MAIN.DESCRIPTION");
  const canonical = buildCanonical(canonicalOrigin, locale, params);
  const hreflangPath = buildPathWithoutLocale(params);

  return {
    title,
    description,
    canonical,
    ...buildCommonSeoProps({
      title,
      description,
      canonical,
      hreflangPath,
      canonicalOrigin,
      locale,
      ogType: "website",
    }),
  };
}

/**
 * SEO props for schedule search results pages (one-way and round-trip).
 */
export function buildScheduleSearchSeo(
  t: TFunction,
  params: ScheduleParams & { type: "route" | "roundtrip" },
  locale: string,
  canonicalOrigin: string,
  cityNames?: CityNames,
): SeoHeadProps {
  const outbound = params.outbound;
  const departureCity = cityNames?.departure ?? outbound.departure;
  const arrivalCity = cityNames?.arrival ?? outbound.arrival;
  const dateFromDisplay = formatDateForSeo(outbound.dateFrom);
  const dateToDisplay = formatDateForSeo(outbound.dateTo);

  const title = t("SEO.SCHEDULE.SEARCH.TITLE", {
    departureCity,
    arrivalCity,
    dateFrom: dateFromDisplay,
    dateTo: dateToDisplay,
  });
  const description = t("SEO.SCHEDULE.SEARCH.DESCRIPTION", {
    departureCity,
    arrivalCity,
    dateFrom: dateFromDisplay,
    dateTo: dateToDisplay,
  });
  const canonical = buildCanonical(canonicalOrigin, locale, params);
  const hreflangPath = buildPathWithoutLocale(params);

  return {
    title,
    description,
    canonical,
    ...buildCommonSeoProps({
      title,
      description,
      canonical,
      hreflangPath,
      canonicalOrigin,
      locale,
      ogType: "website",
    }),
  };
}

/**
 * SEO props for schedule details page.
 */
export function buildScheduleDetailsSeo(
  t: TFunction,
  flights: ISimpleFlight[],
  locale: string,
  canonicalOrigin: string,
  flightIds: IScheduleFlightId[],
): SeoHeadProps {
  const flightDisplay = flightIds
    .map((f) => `${f.carrier} ${f.flightNumber}${f.suffix ?? ""}`)
    .join(", ");
  const dateDisplay = flightIds[0]
    ? formatDateForSeo(flightIds[0].date)
    : "";

  const title = t("SEO.SCHEDULE.DETAILS.TITLE", {
    flights: flightDisplay,
    date: dateDisplay,
  });
  const description = t("SEO.SCHEDULE.DETAILS.DESCRIPTION", {
    flights: flightDisplay,
    date: dateDisplay,
  });

  const detailsParams: ScheduleParams = { type: "details", flights: flightIds };
  const canonical = buildCanonical(canonicalOrigin, locale, detailsParams);
  const hreflangPath = buildPathWithoutLocale(detailsParams);

  return {
    title,
    description,
    canonical,
    ...buildCommonSeoProps({
      title,
      description,
      canonical,
      hreflangPath,
      canonicalOrigin,
      locale,
      ogType: "article",
    }),
  };
}
