/**
 * Data model types for the Online Board feature.
 *
 * Ported from Angular typings (ClientApp/src/typings/) — flattened into
 * minimal, UI-oriented interfaces with no Angular dependencies.
 */

// ---------------------------------------------------------------------------
// Enums & literals
// ---------------------------------------------------------------------------

/** Flight status values returned by the API */
export type FlightStatus =
  | "Scheduled"
  | "Sent"
  | "InFlight"
  | "Landed"
  | "Arrived"
  | "Delayed"
  | "Cancelled"
  | "Unknown";

/** Route shape discriminator */
export type RouteType = "Direct" | "MultiLeg" | "Connecting";

/** Search request type — how the user is searching */
export type FlightRequestType = "flight" | "departure" | "arrival" | "route";

// ---------------------------------------------------------------------------
// Time & location primitives
// ---------------------------------------------------------------------------

/** Day-change indicator (e.g. +1, -1 day vs scheduled) */
export interface IDayChange {
  value: number;
  title: string;
}

/** A single point-in-time with timezone info, as returned by the API */
export interface ITimesSet {
  dayChange: IDayChange;
  local: string;
  localTime: string;
  tzOffset: number;
  utc: string;
}

/** Airport/city info from the API */
export interface IAirportInfo {
  airport: string;
  airportCode: string;
  city: string;
  cityCode: string;
  countryCode: string;
  country?: string;
}

// ---------------------------------------------------------------------------
// Station (departure / arrival)
// ---------------------------------------------------------------------------

export interface IFlightLegStation {
  scheduled: IAirportInfo;
  latest?: IAirportInfo;
  dispatch?: string;
  gate?: string;
  terminal?: string;
}

export interface IDepartureStationTimes {
  scheduledDeparture: ITimesSet;
  estimatedBlockOff?: ITimesSet;
  actualBlockOff?: ITimesSet;
}

export interface IArrivalStationTimes {
  scheduledArrival: ITimesSet;
  estimatedBlockOn?: ITimesSet;
  actualBlockOn?: ITimesSet;
}

export interface IFlightLegDepartureStation extends IFlightLegStation {
  checkingStatus: string;
  parkingStand?: string;
  times: IDepartureStationTimes;
}

export interface IFlightLegArrivalStation extends IFlightLegStation {
  times: IArrivalStationTimes;
  bagBelt?: string;
}

// ---------------------------------------------------------------------------
// Flight leg
// ---------------------------------------------------------------------------

export interface IFlightLegFlags {
  checkinAvailable: boolean;
  returnToAirport: boolean;
  routeChanged: boolean;
}

// ---------------------------------------------------------------------------
// Days of week
// ---------------------------------------------------------------------------

/**
 * Bit-string indicator of flight operation days.
 * Position 0 = Monday, position 6 = Sunday. `"1"` means active.
 */
export interface IDaysOfWeek {
  current: string;
  flight: string;
}

// ---------------------------------------------------------------------------
// Duration
// ---------------------------------------------------------------------------

/**
 * Structured duration for flight timings. `isNegative` flags an uncertain
 * or "specifying" state (estimated arrival is earlier than departure).
 */
export interface IDuration {
  days: number;
  hours: number;
  minutes: number;
  isNegative?: boolean;
}

// ---------------------------------------------------------------------------
// Transition (registration/boarding/deboarding)
// ---------------------------------------------------------------------------

export type FlightTransitionStatus =
  | "Finished"
  | "Expected"
  | "InProgress"
  | "Specified"
  | "Scheduled";

export interface IFlightTransitionItem {
  end: ITimesSet;
  start: ITimesSet;
  status: FlightTransitionStatus;
  isActual: boolean;
}

export interface IFlightTransitions {
  registration?: IFlightTransitionItem;
  boarding?: IFlightTransitionItem;
  deboarding?: IFlightTransitionItem;
}

// ---------------------------------------------------------------------------
// Equipment (aircraft + meal + on-board services)
// ---------------------------------------------------------------------------

export type MealType = "Economy" | "Comfort" | "Business" | "Special";

export interface IMealItem {
  type: MealType;
}

export interface IOnBoardService {
  id: number;
  title?: string;
  url?: string;
}

export interface IAircraftInfo {
  title?: string;
  onBoardServices?: IOnBoardService[];
}

export interface IEquipmentFull {
  aircraft?: {
    scheduled?: IAircraftInfo;
    actual?: IAircraftInfo;
    configuration?: string;
  };
  meal?: IMealItem[];
}

export interface IFlightLeg {
  arrival: IFlightLegArrivalStation;
  dayChange: number;
  departure: IFlightLegDepartureStation;
  equipment: { name?: string; code?: string } & IEquipmentFull;
  flags: IFlightLegFlags;
  flyingTime: string;
  index: number;
  operatingBy: { carrier?: string; flightNumber?: string };
  status: FlightStatus;
  updated: string;
  transition?: IFlightTransitions;
  daysOfWeek?: IDaysOfWeek;
  estimatedDuration?: IDuration;
  scheduledDuration?: IDuration;
}

// ---------------------------------------------------------------------------
// Flight ID
// ---------------------------------------------------------------------------

export interface IFlightId {
  carrier: string;
  date: string;
  flightNumber: string;
  suffix: string;
  dateLT?: string;
}

export interface IParsedFlightId {
  carrier: string;
  flightNumber: string;
  suffix?: string;
  date: string;
}

// ---------------------------------------------------------------------------
// Flight (union of route shapes)
// ---------------------------------------------------------------------------

interface IFlightBase {
  flightId: IFlightId;
  flyingTime: string;
  operatingBy: { carrier?: string; flightNumber?: string };
  id: string;
  status: FlightStatus;
}

export interface IDirectFlight extends IFlightBase {
  routeType: "Direct";
  leg: IFlightLeg;
}

export interface IMultiLegFlight extends IFlightBase {
  routeType: "MultiLeg";
  legs: IFlightLeg[];
}

export interface IConnectingFlight {
  flights: ISimpleFlight[];
  routeType: "Connecting";
  flyingTime: string;
  status: FlightStatus;
}

/** A single flight (direct or multi-leg) — the main UI display unit */
export type ISimpleFlight = IDirectFlight | IMultiLegFlight;

/** Any flight shape including connecting */
export type IFlight = ISimpleFlight | IConnectingFlight;

// ---------------------------------------------------------------------------
// API response shapes
// ---------------------------------------------------------------------------

export interface IBoardResponse {
  data: {
    partners: string[];
    routes: ISimpleFlight[];
    daysOfFlight: string[];
  };
}

export interface IDaysResponse {
  days: string;
}
