/**
 * SEO builder functions for Online Board 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 Angular pattern from meta-tags.service.ts:
 * SEO.BOARD.{MAIN,FLIGHT-SEARCH,DEPARTURE-SEARCH,ARRIVAL-SEARCH,ROUTE-SEARCH,FLIGHT-DETAILS}.{TITLE,DESCRIPTION}
 *
 * @module
 */

import type { SeoHeadProps } from "@/ui/seo/SeoHead.js";
import { buildHreflangSet } from "@/shared/seo/hreflang.js";
import { buildOnlineBoardUrl } from "./url.js";
import type { OnlineBoardParams } from "./url.js";
import type { ISimpleFlight } from "./types.js";

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

/**
 * Translation function signature — intentionally loose to accept
 * both i18next's TFunction and simple test stubs.
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type TFunction = (key: string, opts?: any) => string;

/** Optional city names for station/route searches */
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
// ---------------------------------------------------------------------------

/**
 * Format a yyyyMMdd date string to dd.MM.yyyy for display in SEO strings.
 */
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}`;
}

/**
 * Build the canonical URL for a given set of params + locale.
 */
function buildCanonical(
  canonicalOrigin: string,
  locale: string,
  params: OnlineBoardParams,
): string {
  const path = buildOnlineBoardUrl(params);
  return `${canonicalOrigin}/${locale}/${path}`;
}

/**
 * Build the path without locale prefix for hreflang generation.
 */
function buildPathWithoutLocale(params: OnlineBoardParams): string {
  const path = buildOnlineBoardUrl(params);
  return `/${path}`;
}

/**
 * Assemble the common OG + Twitter fields from title, description, canonical, locale.
 */
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 Online Board start page.
 */
export function buildOnlineBoardStartSeo(
  t: TFunction,
  locale: string,
  canonicalOrigin: string,
): SeoHeadProps {
  const params: OnlineBoardParams = { type: "start" };
  const title = t("SEO.BOARD.MAIN.TITLE");
  const description = t("SEO.BOARD.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 flight number search results page.
 */
export function buildFlightSearchSeo(
  t: TFunction,
  params: { type: "flight"; carrier: string; flightNumber: string; suffix?: string; date: string },
  locale: string,
  canonicalOrigin: string,
): SeoHeadProps {
  const flightDisplay = `${params.carrier} ${params.flightNumber}${params.suffix ?? ""}`;
  const dateDisplay = formatDateForSeo(params.date);

  const title = t("SEO.BOARD.FLIGHT-SEARCH.TITLE", {
    flightNumber: flightDisplay,
    date: dateDisplay,
  });
  const description = t("SEO.BOARD.FLIGHT-SEARCH.DESCRIPTION", {
    flightNumber: flightDisplay,
    date: dateDisplay,
  });
  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 departure search results page.
 */
export function buildDepartureSearchSeo(
  t: TFunction,
  params: { type: "departure"; station: string; date: string; timeFrom?: string; timeTo?: string },
  locale: string,
  canonicalOrigin: string,
  cityNames?: CityNames,
): SeoHeadProps {
  const departureCity = cityNames?.departure ?? params.station;
  const dateDisplay = formatDateForSeo(params.date);

  const title = t("SEO.BOARD.DEPARTURE-SEARCH.TITLE", {
    departureCity,
    date: dateDisplay,
  });
  const description = t("SEO.BOARD.DEPARTURE-SEARCH.DESCRIPTION", {
    departureCity,
    date: dateDisplay,
  });
  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 arrival search results page.
 */
export function buildArrivalSearchSeo(
  t: TFunction,
  params: { type: "arrival"; station: string; date: string; timeFrom?: string; timeTo?: string },
  locale: string,
  canonicalOrigin: string,
  cityNames?: CityNames,
): SeoHeadProps {
  const arrivalCity = cityNames?.arrival ?? params.station;
  const dateDisplay = formatDateForSeo(params.date);

  const title = t("SEO.BOARD.ARRIVAL-SEARCH.TITLE", {
    arrivalCity,
    date: dateDisplay,
  });
  const description = t("SEO.BOARD.ARRIVAL-SEARCH.DESCRIPTION", {
    arrivalCity,
    date: dateDisplay,
  });
  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 route search results page.
 */
export function buildRouteSearchSeo(
  t: TFunction,
  params: { type: "route"; departure: string; arrival: string; date: string; timeFrom?: string; timeTo?: string },
  locale: string,
  canonicalOrigin: string,
  cityNames?: CityNames,
): SeoHeadProps {
  const departureCity = cityNames?.departure ?? params.departure;
  const arrivalCity = cityNames?.arrival ?? params.arrival;
  const dateDisplay = formatDateForSeo(params.date);

  const title = t("SEO.BOARD.ROUTE-SEARCH.TITLE", {
    departureCity,
    arrivalCity,
    date: dateDisplay,
  });
  const description = t("SEO.BOARD.ROUTE-SEARCH.DESCRIPTION", {
    departureCity,
    arrivalCity,
    date: dateDisplay,
  });
  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 flight details page.
 */
export function buildFlightDetailsSeo(
  t: TFunction,
  flight: ISimpleFlight,
  locale: string,
  canonicalOrigin: string,
): SeoHeadProps {
  const { carrier, flightNumber, suffix, date } = flight.flightId;
  const flightDisplay = `${carrier} ${flightNumber}${suffix ?? ""}`;
  const dateDisplay = formatDateForSeo(date);

  const title = t("SEO.BOARD.FLIGHT-DETAILS.TITLE", {
    flightNumber: flightDisplay,
    date: dateDisplay,
  });
  const description = t("SEO.BOARD.FLIGHT-DETAILS.DESCRIPTION", {
    flightNumber: flightDisplay,
    date: dateDisplay,
  });

  const detailsParams: OnlineBoardParams = suffix
    ? { type: "details", carrier, flightNumber, suffix, date }
    : { type: "details", carrier, flightNumber, date };
  const canonical = buildCanonical(canonicalOrigin, locale, detailsParams);
  const hreflangPath = buildPathWithoutLocale(detailsParams);

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