import { describe, it, expect } from "vitest";
import { canRegister } from "./registrationVisibility.js";
import { AIRLINES } from "../airlines.js";
import type { ISimpleFlight, FlightTransitionStatus } from "../../../types.js";

function makeFlight(carrier: string, regStatus?: FlightTransitionStatus): ISimpleFlight {
  const legBase = {
    arrival: {
      scheduled: { airport: "", airportCode: "", city: "", cityCode: "", countryCode: "" },
      latest: { airport: "", airportCode: "", city: "", cityCode: "", countryCode: "" },
      dispatch: "", gate: "", terminal: "",
      times: { scheduledArrival: { dayChange: { value: 0, title: "" }, local: "", localTime: "", tzOffset: 0, utc: "" } },
    },
    dayChange: 0,
    departure: {
      scheduled: { airport: "", airportCode: "", city: "", cityCode: "", countryCode: "" },
      latest: { airport: "", airportCode: "", city: "", cityCode: "", countryCode: "" },
      dispatch: "", gate: "", terminal: "", checkingStatus: "Scheduled", parkingStand: "",
      times: { scheduledDeparture: { dayChange: { value: 0, title: "" }, local: "", localTime: "", tzOffset: 0, utc: "" } },
    },
    equipment: {},
    flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
    flyingTime: "1h",
    index: 0,
    operatingBy: {},
    status: "Scheduled" as const,
    updated: "",
  };

  const leg = regStatus
    ? {
        ...legBase,
        transition: {
          registration: {
            start: {} as never,
            end: {} as never,
            status: regStatus,
            isActual: true,
          },
        },
      }
    : legBase;

  return {
    id: "X-1",
    routeType: "Direct",
    flyingTime: "1h",
    status: "Scheduled",
    flightId: { carrier, flightNumber: "0022", suffix: "", date: "20260417" },
    operatingBy: { carrier, flightNumber: "0022" },
    leg,
  } as ISimpleFlight;
}

describe("canRegister", () => {
  it("returns true for SU with InProgress registration", () => {
    expect(canRegister(makeFlight("SU", "InProgress"), AIRLINES)).toBe(true);
  });

  it("returns false for SU without InProgress registration", () => {
    expect(canRegister(makeFlight("SU", "Scheduled"), AIRLINES)).toBe(false);
  });

  it("returns false for SU with no registration transition", () => {
    expect(canRegister(makeFlight("SU"), AIRLINES)).toBe(false);
  });

  it("returns false for AF (no registration URL)", () => {
    expect(canRegister(makeFlight("AF", "InProgress"), AIRLINES)).toBe(false);
  });

  it("returns false for unknown carrier", () => {
    expect(canRegister(makeFlight("XX", "InProgress"), AIRLINES)).toBe(false);
  });
});
