/**
 * Tests for FlightsMiniListItem — a single row in the flights mini-list sidebar.
 *
 * @vitest-environment jsdom
 */

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

vi.mock("@modern-js/runtime/router", () => ({
  Link: ({ children, to, ...props }: { children: React.ReactNode; to: string; className?: string; [k: string]: unknown }) => (
    <a href={to} {...props}>{children}</a>
  ),
}));

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

function makeDirectFlight(overrides: Partial<ISimpleFlight> = {}): ISimpleFlight {
  return {
    id: "SU0022-20260416",
    routeType: "Direct" as const,
    flyingTime: "1h 30m",
    status: "Scheduled" as const,
    flightId: { carrier: "SU", flightNumber: "0022", suffix: "", date: "20260416" },
    operatingBy: {},
    leg: {
      arrival: {
        scheduled: { airport: "Pulkovo", airportCode: "LED", city: "St Petersburg", cityCode: "LED", countryCode: "RU" },
        latest: { airport: "Pulkovo", airportCode: "LED", city: "St Petersburg", cityCode: "LED", countryCode: "RU" },
        dispatch: "", gate: "", terminal: "",
        times: { scheduledArrival: { dayChange: { value: 0, title: "" }, local: "12:30", localTime: "12:30", tzOffset: 3, utc: "09:30" } },
      },
      dayChange: 0,
      departure: {
        scheduled: { airport: "Sheremetyevo", airportCode: "SVO", city: "Moscow", cityCode: "MOW", countryCode: "RU" },
        latest: { airport: "Sheremetyevo", airportCode: "SVO", city: "Moscow", cityCode: "MOW", countryCode: "RU" },
        dispatch: "", gate: "", terminal: "", checkingStatus: "Scheduled", parkingStand: "",
        times: { scheduledDeparture: { dayChange: { value: 0, title: "" }, local: "10:00", localTime: "10:00", tzOffset: 3, utc: "07:00" } },
      },
      equipment: {},
      flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
      flyingTime: "1h 30m",
      index: 0,
      operatingBy: {},
      status: "Scheduled" as const,
      updated: "",
    },
    ...overrides,
  } as ISimpleFlight;
}

describe("FlightsMiniListItem", () => {
  it("renders flight number", () => {
    const flight = makeDirectFlight();
    render(<FlightsMiniListItem flight={flight} isSelected={false} lang="ru" />);
    expect(screen.getByText(/SU\s*0022/)).toBeTruthy();
  });

  it("renders departure and arrival times", () => {
    const flight = makeDirectFlight();
    render(<FlightsMiniListItem flight={flight} isSelected={false} lang="ru" />);
    expect(screen.getByText("10:00")).toBeTruthy();
    expect(screen.getByText("12:30")).toBeTruthy();
  });

  it("renders departure and arrival station codes", () => {
    const flight = makeDirectFlight();
    render(<FlightsMiniListItem flight={flight} isSelected={false} lang="ru" />);
    expect(screen.getByText("SVO")).toBeTruthy();
    expect(screen.getByText("LED")).toBeTruthy();
  });

  it("has data-testid based on flight id", () => {
    const flight = makeDirectFlight();
    render(<FlightsMiniListItem flight={flight} isSelected={false} lang="ru" />);
    expect(screen.getByTestId("mini-list-item-SU0022-20260416")).toBeTruthy();
  });

  it("applies selected modifier when isSelected", () => {
    const flight = makeDirectFlight();
    render(<FlightsMiniListItem flight={flight} isSelected={true} lang="ru" />);
    const item = screen.getByTestId("mini-list-item-SU0022-20260416");
    expect(item.className).toMatch(/--selected/);
  });

  it("does not apply selected modifier when not selected", () => {
    const flight = makeDirectFlight();
    render(<FlightsMiniListItem flight={flight} isSelected={false} lang="ru" />);
    const item = screen.getByTestId("mini-list-item-SU0022-20260416");
    expect(item.className).not.toMatch(/--selected/);
  });

  it("link points to the flight details URL", () => {
    const flight = makeDirectFlight();
    render(<FlightsMiniListItem flight={flight} isSelected={false} lang="ru" />);
    const link = screen.getByTestId("mini-list-item-SU0022-20260416") as HTMLAnchorElement;
    expect(link.getAttribute("href")).toBe("/ru/onlineboard/SU0022-20260416");
  });
});
