// @vitest-environment jsdom
import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { ScheduleLegDetails } from "./ScheduleLegDetails.js";
import type { ISimpleFlight, MealType } from "@/features/online-board/types.js";

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

function makeFlight(meal: MealType[] | undefined): ISimpleFlight {
  return {
    routeType: "Direct",
    id: "x",
    flightId: { carrier: "SU", flightNumber: "6188", date: "20260426" } as never,
    operatingBy: { scheduled: "SU" } as never,
    flyingTime: "01:30:00",
    status: "Scheduled" as never,
    leg: {
      index: 0,
      dayChange: 0,
      flyingTime: "01:30:00",
      updated: "",
      operatingBy: { scheduled: "SU" } as never,
      status: "Scheduled" as never,
      flags: {} as never,
      departure: {
        scheduled: { airport: "VKO", airportCode: "VKO", city: "Москва", cityCode: "MOW", countryCode: "RU" },
        times: { scheduledDeparture: { local: "2026-04-26T00:30:00+03:00", localTime: "00:30", tzOffset: 0, utc: "2026-04-25T21:30:00", dayChange: { value: 0 } as never } },
      } as never,
      arrival: {
        scheduled: { airport: "LED", airportCode: "LED", city: "Санкт-Петербург", cityCode: "LED", countryCode: "RU" },
        times: { scheduledArrival: { local: "2026-04-26T02:00:00+03:00", localTime: "02:00", tzOffset: 0, utc: "2026-04-25T23:00:00", dayChange: { value: 0 } as never } },
      } as never,
      equipment: {
        name: "Sukhoi SuperJet 100",
        aircraft: { scheduled: { title: "Sukhoi SuperJet 100" } },
        ...(meal !== undefined ? { meal: meal.map((type) => ({ type })) } : {}),
      } as never,
    } as never,
  } as unknown as ISimpleFlight;
}

describe("ScheduleLegDetails Питание sub-icons", () => {
  it("renders no meal-class sub-icons when equipment.meal is empty", () => {
    render(<ScheduleLegDetails flight={makeFlight([])} />);
    expect(screen.queryByText("FOOD.ECONOMY")).toBeNull();
    expect(screen.queryByText("FOOD.COMFORT")).toBeNull();
    expect(screen.queryByText("FOOD.BUSINESS")).toBeNull();
    // The cutlery-row label still renders.
    expect(screen.getByText("SHARED.FOOD")).toBeTruthy();
  });

  it("renders only the meal classes the flight actually serves", () => {
    render(<ScheduleLegDetails flight={makeFlight(["Economy", "Business"])} />);
    expect(screen.getByText("FOOD.ECONOMY")).toBeTruthy();
    expect(screen.queryByText("FOOD.COMFORT")).toBeNull();
    expect(screen.getByText("FOOD.BUSINESS")).toBeTruthy();
  });

  it("renders all three when the flight serves all three", () => {
    render(<ScheduleLegDetails flight={makeFlight(["Economy", "Comfort", "Business"])} />);
    expect(screen.getByText("FOOD.ECONOMY")).toBeTruthy();
    expect(screen.getByText("FOOD.COMFORT")).toBeTruthy();
    expect(screen.getByText("FOOD.BUSINESS")).toBeTruthy();
  });
});
