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

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

function makeLeg(overrides: Partial<IFlightLeg> = {}): IFlightLeg {
  const base: IFlightLeg = {
    arrival: {
      scheduled: { airport: "SVO", airportCode: "SVO", city: "Moscow", cityCode: "MOW", countryCode: "RU" },
      latest: { airport: "SVO", airportCode: "SVO", city: "Moscow", cityCode: "MOW", countryCode: "RU" },
      dispatch: "",
      gate: "",
      terminal: "",
      times: { scheduledArrival: { dayChange: { value: 0, title: "" }, local: "", localTime: "", tzOffset: 0, utc: "" } },
    },
    dayChange: 0,
    departure: {
      scheduled: { airport: "JFK", airportCode: "JFK", city: "New York", cityCode: "NYC", countryCode: "US" },
      latest: { airport: "JFK", airportCode: "JFK", city: "New York", cityCode: "NYC", countryCode: "US" },
      dispatch: "",
      gate: "",
      terminal: "",
      checkingStatus: "Scheduled",
      parkingStand: "",
      times: { scheduledDeparture: { dayChange: { value: 0, title: "" }, local: "", localTime: "", tzOffset: 0, utc: "" } },
    },
    equipment: { name: "A320" },
    flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
    flyingTime: "1h 30m",
    index: 0,
    operatingBy: {},
    status: "Scheduled",
    updated: "",
  };
  return { ...base, ...overrides };
}

describe("FlightDetailsAccordion", () => {
  it("returns null when no panels should be visible", () => {
    const leg = makeLeg();
    const { container } = render(<FlightDetailsAccordion leg={leg} viewType="Onlineboard" />);
    expect(container.firstChild).toBeNull();
  });

  it("renders registration tab when transition.registration is active", () => {
    const leg = makeLeg({
      transition: {
        registration: {
          start: { dayChange: { value: 0, title: "" }, local: "10:00", localTime: "10:00", tzOffset: 0, utc: "" },
          end: { dayChange: { value: 0, title: "" }, local: "10:30", localTime: "10:30", tzOffset: 0, utc: "" },
          status: "InProgress",
          isActual: true,
        },
      },
    });
    render(<FlightDetailsAccordion leg={leg} viewType="Onlineboard" />);
    expect(screen.getByTestId("flight-details-accordion")).toBeTruthy();
    expect(screen.getByText("DETAILS.REGISTRATION")).toBeTruthy();
  });

  it("hides transition panels when viewType is Schedule", () => {
    const leg = makeLeg({
      transition: {
        registration: {
          start: { dayChange: { value: 0, title: "" }, local: "10:00", localTime: "", tzOffset: 0, utc: "" },
          end: { dayChange: { value: 0, title: "" }, local: "", localTime: "", tzOffset: 0, utc: "" },
          status: "InProgress",
          isActual: true,
        },
      },
    });
    const { container } = render(<FlightDetailsAccordion leg={leg} viewType="Schedule" />);
    expect(container.firstChild).toBeNull();
  });

  it("renders aircraft tab when equipment.aircraft has title", () => {
    const leg = makeLeg({
      equipment: { name: "A320", aircraft: { actual: { title: "Airbus A320" } } },
    });
    render(<FlightDetailsAccordion leg={leg} viewType="Onlineboard" />);
    expect(screen.getByText("DETAILS.AIRCRAFT")).toBeTruthy();
  });

  it("renders meal tab when equipment.meal has items", () => {
    const leg = makeLeg({
      equipment: { name: "A320", meal: [{ type: "Economy" }] },
    });
    render(<FlightDetailsAccordion leg={leg} viewType="Onlineboard" />);
    expect(screen.getByText("DETAILS.MEAL")).toBeTruthy();
  });

  it("renders services tab when aircraft.actual.onBoardServices has items", () => {
    const leg = makeLeg({
      equipment: { name: "A320", aircraft: { actual: { title: "A320", onBoardServices: [{ id: 1 }] } } },
    });
    render(<FlightDetailsAccordion leg={leg} viewType="Onlineboard" />);
    expect(screen.getByText("DETAILS.ON_BOARD_SERVICES")).toBeTruthy();
  });
});
