// @vitest-environment jsdom
import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import type { IFlightLeg } from "../../types.js";

vi.mock("@/i18n/provider.js", () => ({
  useTranslation: () => ({ t: (k: string) => k }),
}));

vi.mock("../FullRouteTimeline/StationChange.js", () => ({
  StationChange: ({ from, to }: { from: { scheduled: { cityCode: string } }; to: { scheduled: { cityCode: string } } }) => (
    <div data-testid="station-change">{from.scheduled.cityCode}-{to.scheduled.cityCode}</div>
  ),
}));

import { TransferBar } from "./TransferBar.js";

function makeLeg(overrides: {
  arrCity?: string;
  depCity?: string;
  arrCityCode?: string;
  depCityCode?: string;
  arrAirportCode?: string;
  depAirportCode?: string;
  arrUtc?: string;
  depUtc?: string;
  arrLocal?: string;
  depLocal?: string;
} = {}): IFlightLeg {
  const arrCity = overrides.arrCity ?? "Moscow";
  const depCity = overrides.depCity ?? "Moscow";
  const arrCityCode = overrides.arrCityCode ?? "MOW";
  const depCityCode = overrides.depCityCode ?? "MOW";
  const arrAirportCode = overrides.arrAirportCode ?? "SVO";
  const depAirportCode = overrides.depAirportCode ?? "SVO";
  const arrUtc = overrides.arrUtc ?? "2026-04-17T10:00:00Z";
  const depUtc = overrides.depUtc ?? "2026-04-17T11:30:00Z";
  const arrLocal = overrides.arrLocal ?? "10:00";
  const depLocal = overrides.depLocal ?? "11:30";

  return {
    arrival: {
      scheduled: {
        airport: "",
        airportCode: arrAirportCode,
        city: arrCity,
        cityCode: arrCityCode,
        countryCode: "RU",
      },
      terminal: "",
      times: {
        scheduledArrival: {
          dayChange: { value: 0, title: "" },
          local: arrLocal,
          localTime: arrLocal,
          tzOffset: 0,
          utc: arrUtc,
        },
      },
    },
    departure: {
      scheduled: {
        airport: "",
        airportCode: depAirportCode,
        city: depCity,
        cityCode: depCityCode,
        countryCode: "RU",
      },
      terminal: "",
      checkingStatus: "",
      times: {
        scheduledDeparture: {
          dayChange: { value: 0, title: "" },
          local: depLocal,
          localTime: depLocal,
          tzOffset: 0,
          utc: depUtc,
        },
      },
    },
    dayChange: 0,
    equipment: {},
    flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
    flyingTime: "1h 30m",
    index: 0,
    operatingBy: {},
    status: "Scheduled",
    updated: "",
  } as IFlightLeg;
}

describe("TransferBar", () => {
  it("renders with data-testid=transfer-bar", () => {
    const leg = makeLeg({ arrUtc: "2026-04-17T10:00:00Z", arrLocal: "10:00" });
    const nextLeg = makeLeg({ depUtc: "2026-04-17T11:30:00Z", depLocal: "11:30" });
    render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" />);
    expect(screen.getByTestId("transfer-bar")).toBeTruthy();
  });

  it("renders SHARED.INTERMEDIATE-LANDING-PLURAL-ONE label and both times", () => {
    const leg = makeLeg({ arrLocal: "10:00", arrUtc: "2026-04-17T10:00:00Z" });
    const nextLeg = makeLeg({ depLocal: "11:30", depUtc: "2026-04-17T11:30:00Z" });
    render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" />);
    expect(screen.getByText("SHARED.INTERMEDIATE-LANDING-PLURAL-ONE")).toBeTruthy();
    expect(screen.getByText("10:00")).toBeTruthy();
    expect(screen.getByText("11:30")).toBeTruthy();
  });

  it("renders SHARED.FLIGHT-TRANSFER-PLURAL-ONE label when isIntermediateLanding=false", () => {
    const leg = makeLeg({ arrLocal: "10:00", arrUtc: "2026-04-17T10:00:00Z" });
    const nextLeg = makeLeg({ depLocal: "11:30", depUtc: "2026-04-17T11:30:00Z" });
    render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" isIntermediateLanding={false} />);
    expect(screen.getByText("SHARED.FLIGHT-TRANSFER-PLURAL-ONE")).toBeTruthy();
  });

  it("renders TransferTime component", () => {
    const leg = makeLeg({ arrUtc: "2026-04-17T10:00:00Z" });
    const nextLeg = makeLeg({ depUtc: "2026-04-17T11:30:00Z" });
    render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" />);
    expect(screen.getByTestId("transfer-time")).toBeTruthy();
  });

  it("always renders StationChange (station info shown even for same station)", () => {
    const leg = makeLeg({ arrCity: "Moscow", arrCityCode: "MOW", arrAirportCode: "SVO" });
    const nextLeg = makeLeg({ depCity: "Moscow", depCityCode: "MOW", depAirportCode: "SVO" });
    render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" />);
    expect(screen.getByTestId("station-change")).toBeTruthy();
  });

  it("renders StationChange when arrival/departure cities differ", () => {
    const leg = makeLeg({ arrCity: "Moscow", arrCityCode: "MOW" });
    const nextLeg = makeLeg({ depCity: "Saint Petersburg", depCityCode: "LED", depAirportCode: "LED" });
    render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" />);
    expect(screen.getByTestId("station-change")).toBeTruthy();
  });

  it("omits --separated class when arrival/departure cities match", () => {
    const leg = makeLeg({ arrCity: "Moscow", arrCityCode: "MOW", arrAirportCode: "SVO" });
    const nextLeg = makeLeg({ depCity: "Moscow", depCityCode: "MOW", depAirportCode: "SVO" });
    const { container } = render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" />);
    const bar = container.querySelector(".transfer-bar");
    expect(bar?.className).not.toContain("transfer-bar--separated");
  });

  it("adds --separated class when arrival/departure airports differ", () => {
    const leg = makeLeg({ arrCity: "Moscow", arrCityCode: "MOW", arrAirportCode: "SVO" });
    const nextLeg = makeLeg({ depCity: "Saint Petersburg", depCityCode: "LED", depAirportCode: "LED" });
    const { container } = render(<TransferBar leg={leg} nextLeg={nextLeg} viewType="Schedule" />);
    const bar = container.querySelector(".transfer-bar");
    expect(bar?.className).toContain("transfer-bar--separated");
  });
});
