import { describe, it, expect } from "vitest";
import { detectStationChange } from "./detectStationChange.js";
import type { IFlightLegStation } from "../../types.js";

function makeStation(overrides: {
  cityCode?: string;
  airportCode?: string;
  terminal?: string;
}): IFlightLegStation {
  return {
    scheduled: {
      airport: "",
      airportCode: overrides.airportCode ?? "SVO",
      city: "",
      cityCode: overrides.cityCode ?? "MOW",
      countryCode: "RU",
    },
    latest: {
      airport: "",
      airportCode: overrides.airportCode ?? "SVO",
      city: "",
      cityCode: overrides.cityCode ?? "MOW",
      countryCode: "RU",
    },
    dispatch: "",
    gate: "",
    terminal: overrides.terminal ?? "",
  } as IFlightLegStation;
}

describe("detectStationChange", () => {
  it("returns 'noChange' when city/airport/terminal all match", () => {
    const a = makeStation({ cityCode: "MOW", airportCode: "SVO", terminal: "D" });
    const b = makeStation({ cityCode: "MOW", airportCode: "SVO", terminal: "D" });
    expect(detectStationChange(a, b)).toBe("noChange");
  });

  it("returns 'city' when cityCode differs", () => {
    const a = makeStation({ cityCode: "MOW", airportCode: "SVO" });
    const b = makeStation({ cityCode: "LED", airportCode: "LED" });
    expect(detectStationChange(a, b)).toBe("city");
  });

  it("returns 'airport' when cities match but airport differs", () => {
    const a = makeStation({ cityCode: "MOW", airportCode: "SVO" });
    const b = makeStation({ cityCode: "MOW", airportCode: "DME" });
    expect(detectStationChange(a, b)).toBe("airport");
  });

  it("returns 'terminal' when airport matches but terminal differs", () => {
    const a = makeStation({ cityCode: "MOW", airportCode: "SVO", terminal: "D" });
    const b = makeStation({ cityCode: "MOW", airportCode: "SVO", terminal: "F" });
    expect(detectStationChange(a, b)).toBe("terminal");
  });

  it("returns 'noChange' when terminals are both empty strings", () => {
    const a = makeStation({ cityCode: "MOW", airportCode: "SVO", terminal: "" });
    const b = makeStation({ cityCode: "MOW", airportCode: "SVO", terminal: "" });
    expect(detectStationChange(a, b)).toBe("noChange");
  });
});
