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

vi.mock("@/i18n/provider.js", () => ({ useTranslation: () => ({ t: (k: string) => k }) }));
vi.mock("@/shared/hooks/useAppSettings.js", () => ({
  useAppSettings: () => ({
    onlineboardSearchFrom: 2,
    onlineboardSearchTo: 14,
    scheduleSearchFrom: 30,
    scheduleSearchTo: 30,
    flightStatusAvailableFromHours: 24,
    buyTicketMinHours: 2,
    buyTicketMaxHours: 72,
    loading: false,
    error: null,
  }),
}));

function makeLeg(flags: Partial<IFlightLegFlags> = {}): IFlightLeg {
  return {
    arrival: {
      scheduled: { airport: "", airportCode: "LED", city: "", cityCode: "", countryCode: "" },
      latest: { airport: "", airportCode: "LED", city: "", cityCode: "", countryCode: "" },
      dispatch: "",
      gate: "",
      terminal: "",
      times: {
        scheduledArrival: {
          dayChange: { value: 0, title: "" },
          local: "",
          localTime: "",
          tzOffset: 0,
          utc: "",
        },
      },
    },
    dayChange: 0,
    departure: {
      scheduled: { airport: "", airportCode: "SVO", city: "", cityCode: "", countryCode: "" },
      latest: { airport: "", airportCode: "SVO", city: "", cityCode: "", countryCode: "" },
      dispatch: "",
      gate: "",
      terminal: "",
      checkingStatus: "Scheduled",
      parkingStand: "",
      times: {
        scheduledDeparture: {
          dayChange: { value: 0, title: "" },
          local: "",
          localTime: "",
          tzOffset: 0,
          utc: "2026-04-20T10:00:00Z",
        },
      },
    },
    equipment: {},
    flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false, ...flags },
    flyingTime: "1h",
    index: 0,
    operatingBy: {},
    status: "Scheduled",
    updated: "2026-04-20T09:00:00Z",
  } as IFlightLeg;
}

function makeFlight(flags: Partial<IFlightLegFlags> = {}): ISimpleFlight {
  return {
    id: "X",
    routeType: "Direct",
    flyingTime: "1h",
    status: "Scheduled",
    flightId: { carrier: "SU", flightNumber: "0022", suffix: "", date: "20260417" },
    operatingBy: { carrier: "SU", flightNumber: "0022" },
    leg: makeLeg(flags),
  } as ISimpleFlight;
}

describe("BoardDetailsHeader", () => {
  it("has data-testid=board-details-header", () => {
    render(<BoardDetailsHeader flight={makeFlight()} locale="ru" />);
    expect(screen.getByTestId("board-details-header")).toBeTruthy();
  });

  it("renders operator-logo, flight-actions, last-update-timestamp", () => {
    render(<BoardDetailsHeader flight={makeFlight()} locale="ru" />);
    expect(screen.getByTestId("operator-logo")).toBeTruthy();
    expect(screen.getByTestId("flight-actions")).toBeTruthy();
    expect(screen.getByTestId("last-update-timestamp")).toBeTruthy();
  });

  it("renders change-route event when leg.flags.routeChanged=true", () => {
    render(<BoardDetailsHeader flight={makeFlight({ routeChanged: true })} locale="ru" />);
    expect(screen.getByTestId("flight-event-change-route")).toBeTruthy();
  });

  it("renders reroute event when leg.flags.returnToAirport=true", () => {
    render(<BoardDetailsHeader flight={makeFlight({ returnToAirport: true })} locale="ru" />);
    expect(screen.getByTestId("flight-event-reroute")).toBeTruthy();
  });

  it("does not render events when both flags are false", () => {
    render(<BoardDetailsHeader flight={makeFlight()} locale="ru" />);
    expect(screen.queryByTestId("flight-event-change-route")).toBeNull();
    expect(screen.queryByTestId("flight-event-reroute")).toBeNull();
  });
});
