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

vi.mock("@/i18n/provider.js", () => ({
  useTranslation: () => ({
    t: (k: string) => {
      if (k === "SHARED.NOTE-TIME-SCHEDULE") {
        return "Valid from {START_DATE} to {END_DATE}";
      }
      return k;
    },
  }),
}));

interface FlightOverrides {
  daysOfWeek?: { current: string; flight: string };
  depLocal?: string;
  arrLocal?: string;
  flyingTime?: string;
}

function makeFlight(overrides: FlightOverrides = {}): ISimpleFlight {
  const depLocal = overrides.depLocal ?? "2026-04-15T10:00:00";
  const arrLocal = overrides.arrLocal ?? "12:30";
  const flyingTime = overrides.flyingTime ?? "2h 30m";
  return {
    id: "X",
    routeType: "Direct",
    flyingTime,
    status: "Scheduled",
    flightId: { carrier: "SU", flightNumber: "0022", suffix: "", date: "20260415" },
    operatingBy: { carrier: "SU", flightNumber: "0022" },
    leg: {
      arrival: {
        scheduled: {
          airport: "LED",
          airportCode: "LED",
          city: "SPB",
          cityCode: "LED",
          countryCode: "RU",
        },
        latest: {
          airport: "LED",
          airportCode: "LED",
          city: "SPB",
          cityCode: "LED",
          countryCode: "RU",
        },
        dispatch: "",
        gate: "",
        terminal: "",
        times: {
          scheduledArrival: {
            dayChange: { value: 0, title: "" },
            local: arrLocal,
            localTime: "",
            tzOffset: 0,
            utc: "",
          },
        },
      },
      dayChange: 0,
      departure: {
        scheduled: {
          airport: "SVO",
          airportCode: "SVO",
          city: "MOW",
          cityCode: "MOW",
          countryCode: "RU",
        },
        latest: {
          airport: "SVO",
          airportCode: "SVO",
          city: "MOW",
          cityCode: "MOW",
          countryCode: "RU",
        },
        dispatch: "",
        gate: "",
        terminal: "",
        checkingStatus: "Scheduled",
        parkingStand: "",
        times: {
          scheduledDeparture: {
            dayChange: { value: 0, title: "" },
            local: depLocal,
            localTime: "10:00",
            tzOffset: 0,
            utc: "",
          },
        },
      },
      equipment: {},
      flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
      flyingTime,
      index: 0,
      operatingBy: {},
      status: "Scheduled",
      updated: "",
      ...(overrides.daysOfWeek ? { daysOfWeek: overrides.daysOfWeek } : {}),
    },
  } as ISimpleFlight;
}

describe("FlightSchedule", () => {
  it("returns null when firstLeg has no daysOfWeek", () => {
    const { container } = render(<FlightSchedule flight={makeFlight()} />);
    expect(container.firstChild).toBeNull();
  });

  it("returns null when daysOfWeek.flight is empty string", () => {
    const flight = makeFlight({ daysOfWeek: { current: "1111111", flight: "" } });
    const { container } = render(<FlightSchedule flight={flight} />);
    expect(container.firstChild).toBeNull();
  });

  it("renders with data-testid=flight-schedule when daysOfWeek present", () => {
    const flight = makeFlight({
      daysOfWeek: { current: "1111111", flight: "1111111" },
    });
    render(<FlightSchedule flight={flight} />);
    expect(screen.getByTestId("flight-schedule")).toBeTruthy();
  });

  it("renders 7 day boxes from daysOfWeek.flight", () => {
    const flight = makeFlight({
      daysOfWeek: { current: "1110000", flight: "1110000" },
    });
    render(<FlightSchedule flight={flight} />);
    const boxes = screen.getAllByTestId(/^day-of-week-\d$/);
    expect(boxes).toHaveLength(7);
  });

  it("substitutes {START_DATE} and {END_DATE} into the note", () => {
    const flight = makeFlight({
      daysOfWeek: { current: "1111111", flight: "1111111" },
    });
    render(<FlightSchedule flight={flight} />);
    const note = screen.getByTestId("flight-schedule-note");
    expect(note.textContent).toContain("13.04.2026");
    expect(note.textContent).toContain("19.04.2026");
  });

  it("renders scheduled departure/arrival times and duration", () => {
    const flight = makeFlight({
      daysOfWeek: { current: "1111111", flight: "1111111" },
    });
    render(<FlightSchedule flight={flight} />);
    expect(screen.getByText("10:00")).toBeTruthy();
    expect(screen.getByText("12:30")).toBeTruthy();
    expect(screen.getByText("2h 30m")).toBeTruthy();
  });
});
