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

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

// Build an ISO date string that is N days from today (negative = past)
function isoDateDaysFromNow(n: number): string {
  const d = new Date();
  d.setDate(d.getDate() + n);
  return d.toISOString();
}

describe("AircraftPanel", () => {
  // The aircraft title (e.g. 'Airbus A321') is rendered by the wrapping
  // FlightDetailsAccordion row caption, not by the panel itself — Angular
  // parity moved 'Борт: <title>' into the row header. Tests that assert on
  // the title live at the accordion level; here we only cover the body.

  it("renders without throwing when given minimal title-only equipment", () => {
    const eq: IEquipmentFull = { aircraft: { actual: { title: "Airbus A321" } } };
    render(<AircraftPanel equipment={eq} />);
    expect(screen.getByTestId("aircraft-panel")).toBeTruthy();
  });

  it("renders seat totals per class from configuration.seats", () => {
    const eq: IEquipmentFull = {
      aircraft: {
        actual: { title: "Sukhoi SuperJet 100" },
        configuration: {
          seats: [
            { type: "Business", count: 12 },
            { type: "Economy", count: 75 },
          ],
        },
      },
    };
    render(<AircraftPanel equipment={eq} />);
    expect(screen.getByText("87")).toBeTruthy(); // total
    expect(screen.getByText("75")).toBeTruthy(); // economy
    expect(screen.getByText("12")).toBeTruthy(); // business
  });

  it("renders previous-flight identifier when present", () => {
    const eq: IEquipmentFull = {
      aircraft: {
        actual: { title: "A320" },
        previousFlight: {
          localDate: "2026-04-17",
          carrier: "SU",
          flightNumber: "6805",
          date: "2026-04-17",
        },
      },
    };
    render(<AircraftPanel equipment={eq} />);
    expect(screen.getByText("SU 6805")).toBeTruthy();
  });

  it("has data-testid", () => {
    const eq: IEquipmentFull = { aircraft: { actual: { title: "A320" } } };
    render(<AircraftPanel equipment={eq} />);
    expect(screen.getByTestId("aircraft-panel")).toBeTruthy();
  });

  // §4.1.15.4: tail number (registration mark) in aircraft panel
  it("4.1.15.4-TailNumber: renders tail number when aircraft.registration is present", () => {
    const eq: IEquipmentFull = {
      aircraft: { actual: { title: "Airbus A320", registration: "VP-BQS" } },
    };
    render(<AircraftPanel equipment={eq} />);
    expect(screen.getByText("VP-BQS")).toBeTruthy();
    // label key is AIRPLANE.TAIL-NUMBER (t mock returns key)
    expect(screen.getByText("AIRPLANE.TAIL-NUMBER")).toBeTruthy();
  });

  it("4.1.15.4-TailNumber-Absent: omits tail-number row when registration is not present", () => {
    const eq: IEquipmentFull = {
      aircraft: { actual: { title: "Airbus A320" } },
    };
    render(<AircraftPanel equipment={eq} />);
    expect(screen.queryByText("AIRPLANE.TAIL-NUMBER")).toBeNull();
  });

  // §4.1.15.9: previous-flight chip must be a link (opens new tab) when the
  // flight's scheduled departure date is more recent than today − 2 days.
  describe("previous-flight link (§4.1.15.9)", () => {
    const previousFlight = {
      localDate: "2026-04-20",
      carrier: "SU",
      flightNumber: "6805",
      date: "2026-04-20",
    };

    it("renders an <a target=_blank> link when departure is within the last 2 days", () => {
      const eq: IEquipmentFull = {
        aircraft: {
          actual: { title: "A320" },
          previousFlight,
        },
      };
      // departureDateLocal set to today (within the 2-day window)
      render(
        <AircraftPanel
          equipment={eq}
          locale="ru-ru"
          departureDateLocal={isoDateDaysFromNow(0)}
        />,
      );
      const link = screen.getByRole("link", { name: "SU 6805" });
      expect(link).toBeTruthy();
      expect(link.getAttribute("target")).toBe("_blank");
      expect(link.getAttribute("href")).toContain("ru-ru/onlineboard");
      expect(link.getAttribute("href")).toContain("SU6805");
    });

    it("includes the correct date (localDate of previousFlight) in the href", () => {
      const eq: IEquipmentFull = {
        aircraft: {
          actual: { title: "A320" },
          previousFlight: {
            localDate: "2026-04-20",
            carrier: "SU",
            flightNumber: "0100",
            date: "2026-04-20",
          },
        },
      };
      render(
        <AircraftPanel
          equipment={eq}
          locale="ru-ru"
          departureDateLocal={isoDateDaysFromNow(0)}
        />,
      );
      const link = screen.getByRole("link");
      // Date in href must be the compact localDate of the previous flight
      expect(link.getAttribute("href")).toContain("20260420");
    });

    it("renders plain text (no link) when departure is older than 2 days", () => {
      const eq: IEquipmentFull = {
        aircraft: {
          actual: { title: "A320" },
          previousFlight,
        },
      };
      // departureDateLocal 10 days ago — outside the 2-day window
      render(
        <AircraftPanel
          equipment={eq}
          locale="ru-ru"
          departureDateLocal={isoDateDaysFromNow(-10)}
        />,
      );
      expect(screen.getByText("SU 6805")).toBeTruthy();
      expect(screen.queryByRole("link")).toBeNull();
    });

    it("renders plain text when locale is not provided", () => {
      const eq: IEquipmentFull = {
        aircraft: {
          actual: { title: "A320" },
          previousFlight,
        },
      };
      render(
        <AircraftPanel
          equipment={eq}
          departureDateLocal={isoDateDaysFromNow(0)}
        />,
      );
      expect(screen.getByText("SU 6805")).toBeTruthy();
      expect(screen.queryByRole("link")).toBeNull();
    });

    it("renders plain text when departureDateLocal is not provided", () => {
      const eq: IEquipmentFull = {
        aircraft: {
          actual: { title: "A320" },
          previousFlight,
        },
      };
      render(<AircraftPanel equipment={eq} locale="ru-ru" />);
      expect(screen.getByText("SU 6805")).toBeTruthy();
      expect(screen.queryByRole("link")).toBeNull();
    });

    it("includes suffix in the link label and href when present", () => {
      const eq: IEquipmentFull = {
        aircraft: {
          actual: { title: "A320" },
          previousFlight: {
            localDate: "2026-04-20",
            carrier: "SU",
            flightNumber: "1234",
            suffix: "A",
            date: "2026-04-20",
          },
        },
      };
      render(
        <AircraftPanel
          equipment={eq}
          locale="ru-ru"
          departureDateLocal={isoDateDaysFromNow(0)}
        />,
      );
      const link = screen.getByRole("link", { name: "SU 1234A" });
      expect(link).toBeTruthy();
      expect(link.getAttribute("href")).toContain("1234A");
    });
  });
});
