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

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

function makeFlight(
  depUtc = "2026-04-20T10:00:00Z",
  dep = "SVO",
  arr = "LED",
): ISimpleFlight {
  return {
    id: "X",
    routeType: "Direct",
    flyingTime: "1h",
    status: "Scheduled",
    flightId: { carrier: "SU", flightNumber: "0022", suffix: "", date: "20260417" },
    operatingBy: { carrier: "SU", flightNumber: "0022" },
    leg: {
      arrival: {
        scheduled: { airport: "", airportCode: arr, city: "", cityCode: "", countryCode: "" },
        latest: { airport: "", airportCode: arr, city: "", cityCode: "", countryCode: "" },
        dispatch: "",
        gate: "",
        terminal: "",
        times: {
          scheduledArrival: {
            dayChange: { value: 0, title: "" },
            local: "",
            localTime: "",
            tzOffset: 0,
            utc: "",
          },
        },
      },
      dayChange: 0,
      departure: {
        scheduled: { airport: "", airportCode: dep, city: "", cityCode: "", countryCode: "" },
        latest: { airport: "", airportCode: dep, city: "", cityCode: "", countryCode: "" },
        dispatch: "",
        gate: "",
        terminal: "",
        checkingStatus: "Scheduled",
        parkingStand: "",
        times: {
          scheduledDeparture: {
            dayChange: { value: 0, title: "" },
            local: "",
            localTime: "",
            tzOffset: 0,
            utc: depUtc,
          },
        },
      },
      equipment: {},
      flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
      flyingTime: "1h",
      index: 0,
      operatingBy: {},
      status: "Scheduled",
      updated: "",
    },
  } as ISimpleFlight;
}

describe("BuyTicketButton", () => {
  beforeEach(() => {
    Object.defineProperty(window, "open", { value: vi.fn(), writable: true });
  });

  it("renders label SHARED.BUY-TICKET", () => {
    render(<BuyTicketButton flight={makeFlight()} locale="ru" />);
    expect(screen.getByText("SHARED.BUY-TICKET")).toBeTruthy();
  });

  it("has data-testid buy-ticket-button", () => {
    render(<BuyTicketButton flight={makeFlight()} locale="ru" />);
    expect(screen.getByTestId("buy-ticket-button")).toBeTruthy();
  });

  it("click opens aeroflot buy ticket URL with locale, routes, and autosearch", () => {
    render(<BuyTicketButton flight={makeFlight()} locale="ru" />);
    fireEvent.click(screen.getByTestId("buy-ticket-button"));
    const openMock = window.open as ReturnType<typeof vi.fn>;
    expect(openMock).toHaveBeenCalledTimes(1);
    const url = openMock.mock.calls[0]![0] as string;
    expect(url).toContain("aeroflot.ru/sb/app/ru-ru");
    expect(url).toContain("routes=SVO.20260420.LED");
    expect(url).toContain("autosearch=Y");
  });
});
