/**
 * JSON-LD schema builders for Online Board pages.
 *
 * Produces schema-dts typed objects ready for <JsonLdRenderer>.
 * Uses schema.org Flight and ItemList types.
 *
 * @module
 */

import type { Flight, ItemList, ListItem } from "schema-dts";
import type { ISimpleFlight, IFlightLeg } from "./types.js";

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

/**
 * Get the first leg from a flight (handles both Direct and MultiLeg).
 */
function getFirstLeg(flight: ISimpleFlight): IFlightLeg | undefined {
  if (flight.routeType === "Direct") {
    return flight.leg;
  }
  return flight.legs[0];
}

/**
 * Get the last leg from a flight (handles both Direct and MultiLeg).
 */
function getLastLeg(flight: ISimpleFlight): IFlightLeg | undefined {
  if (flight.routeType === "Direct") {
    return flight.leg;
  }
  const { legs } = flight;
  return legs[legs.length - 1];
}

// ---------------------------------------------------------------------------
// Public JSON-LD builders
// ---------------------------------------------------------------------------

/**
 * Build a schema.org Flight JSON-LD object from a flight record.
 *
 * Maps the first leg's departure and last leg's arrival for multi-leg flights.
 */
export function buildFlightJsonLd(flight: ISimpleFlight): Flight {
  const firstLeg = getFirstLeg(flight);
  const lastLeg = getLastLeg(flight);

  const { carrier, flightNumber } = flight.flightId;

  const result: Flight = {
    "@type": "Flight",
    flightNumber: `${carrier}${flightNumber}`,
    provider: {
      "@type": "Airline",
      name: "Aeroflot",
      iataCode: carrier,
    },
    estimatedFlightDuration: flight.flyingTime,
  };

  if (firstLeg) {
    result.departureAirport = {
      "@type": "Airport",
      iataCode: firstLeg.departure.scheduled.airportCode,
      name: firstLeg.departure.scheduled.airport,
    };
    result.departureTime = firstLeg.departure.times.scheduledDeparture.local;

    if (firstLeg.departure.terminal) {
      result.departureTerminal = firstLeg.departure.terminal;
    }
    if (firstLeg.departure.gate) {
      result.departureGate = firstLeg.departure.gate;
    }
  }

  if (lastLeg) {
    result.arrivalAirport = {
      "@type": "Airport",
      iataCode: lastLeg.arrival.scheduled.airportCode,
      name: lastLeg.arrival.scheduled.airport,
    };
    result.arrivalTime = lastLeg.arrival.times.scheduledArrival.local;

    if (lastLeg.arrival.terminal) {
      result.arrivalTerminal = lastLeg.arrival.terminal;
    }
  }

  if (firstLeg?.equipment.name) {
    result.aircraft = firstLeg.equipment.name;
  }

  return result;
}

/**
 * Build a schema.org ItemList of Flight JSON-LD objects from search results.
 *
 * Each flight becomes a ListItem with a position (1-indexed).
 */
export function buildFlightListJsonLd(
  flights: ISimpleFlight[],
  searchDescription: string,
): ItemList {
  const items: ListItem[] = flights.map((flight, index) => ({
    "@type": "ListItem" as const,
    position: index + 1,
    item: buildFlightJsonLd(flight),
  }));

  return {
    "@type": "ItemList",
    description: searchDescription,
    itemListElement: items,
  };
}
