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

vi.mock("@modern-js/runtime/router", () => ({
  Link: forwardRef<
    HTMLAnchorElement,
    React.AnchorHTMLAttributes<HTMLAnchorElement> & { to: string }
  >(({ children, to, ...props }, ref) => (
    <a ref={ref} href={to} {...props}>
      {children}
    </a>
  )),
}));

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

beforeEach(() => {
  Element.prototype.scrollIntoView = vi.fn();
});

function flight(id: string, ymd: string): ISimpleFlight {
  const iso = `${ymd}T10:00:00`;
  const arrIso = `${ymd}T13:00:00`;
  return {
    routeType: "Direct",
    id,
    flightId: { carrier: "SU", flightNumber: "1", date: ymd.replace(/-/g, ""), suffix: "" },
    operatingBy: { scheduled: "SU" },
    flyingTime: "03:00:00",
    status: "Scheduled",
    leg: {
      index: 0,
      dayChange: 0,
      flyingTime: "03:00:00",
      updated: "",
      equipment: {} as never,
      operatingBy: { scheduled: "SU" },
      status: "Scheduled",
      flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
      departure: {
        scheduled: { airport: "SVO", airportCode: "SVO", city: "Moscow", cityCode: "MOW", countryCode: "RU" },
        checkingStatus: "",
        times: {
          scheduledDeparture: { local: iso, localTime: "10:00", tzOffset: 0, utc: iso, dayChange: { value: 0 } as never },
        },
      } as never,
      arrival: {
        scheduled: { airport: "LED", airportCode: "LED", city: "St Petersburg", cityCode: "LED", countryCode: "RU" },
        times: {
          scheduledArrival: { local: arrIso, localTime: "13:00", tzOffset: 0, utc: arrIso, dayChange: { value: 0 } as never },
        },
      } as never,
    } as never,
  } as unknown as ISimpleFlight;
}

describe("ScheduleFlightsMiniList — flat list (no day-grouping accordions)", () => {
  it("renders every input flight as a row, sorted by departure time", () => {
    const a = flight("a", "2026-04-21");
    const b = flight("b", "2026-04-22");
    const c = flight("c", "2026-04-23");
    render(
      <ScheduleFlightsMiniList
        flights={[c, a, b]}
        currentFlight={b}
        lang="en-us"
      />,
    );
    expect(screen.getByTestId("mini-list-item-a")).toBeTruthy();
    expect(screen.getByTestId("mini-list-item-b")).toBeTruthy();
    expect(screen.getByTestId("mini-list-item-c")).toBeTruthy();
  });

  it("falls back to the open flight when search results are empty", () => {
    const cur = flight("only", "2026-04-22");
    render(
      <ScheduleFlightsMiniList
        flights={[]}
        currentFlight={cur}
        lang="en-us"
      />,
    );
    expect(screen.getByTestId("mini-list-item-only")).toBeTruthy();
  });

  it("does NOT render any day-grouping accordion headers", () => {
    const cur = flight("a", "2026-04-22");
    render(
      <ScheduleFlightsMiniList
        flights={[cur]}
        currentFlight={cur}
        lang="en-us"
      />,
    );
    expect(screen.queryAllByTestId(/^mini-list-day-header-/)).toHaveLength(0);
    expect(screen.queryAllByTestId(/^mini-list-day-\d/)).toHaveLength(0);
  });
});
