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

vi.mock("@/i18n/provider.js", () => ({ useTranslation: () => ({ t: (k: string) => k }) }));
vi.mock("@/shared/hooks/useAppSettings.js", () => ({
  useAppSettings: () => ({
    onlineboardSearchFrom: 2,
    onlineboardSearchTo: 14,
    scheduleSearchFrom: 30,
    scheduleSearchTo: 30,
    flightStatusAvailableFromHours: 24,
    buyTicketMinHours: 2,
    buyTicketMaxHours: 72,
    loading: false,
    error: null,
  }),
}));
vi.mock("./visibility/buyTicketVisibility.js", () => ({ canBuyTicket: vi.fn(() => true) }));
vi.mock("./visibility/registrationVisibility.js", () => ({ canRegister: vi.fn(() => true) }));
vi.mock("./visibility/flightStatusVisibility.js", () => ({ canViewFlightStatus: vi.fn(() => true) }));

function makeFlight(): 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: "LED", city: "", cityCode: "", countryCode: "" },
        latest: { airport: "", airportCode: "LED", city: "", cityCode: "", countryCode: "" },
        dispatch: "",
        gate: "",
        terminal: "",
        times: {
          scheduledArrival: {
            dayChange: { value: 0, title: "" },
            local: "",
            localTime: "",
            tzOffset: 0,
            utc: "",
          },
        },
      },
      dayChange: 0,
      departure: {
        scheduled: { airport: "", airportCode: "SVO", city: "", cityCode: "", countryCode: "" },
        latest: { airport: "", airportCode: "SVO", city: "", cityCode: "", countryCode: "" },
        dispatch: "",
        gate: "",
        terminal: "",
        checkingStatus: "Scheduled",
        parkingStand: "",
        times: {
          scheduledDeparture: {
            dayChange: { value: 0, title: "" },
            local: "",
            localTime: "",
            tzOffset: 0,
            utc: "2026-04-20T10:00:00Z",
          },
        },
      },
      equipment: {},
      flags: { checkinAvailable: false, returnToAirport: false, routeChanged: false },
      flyingTime: "1h",
      index: 0,
      operatingBy: {},
      status: "Scheduled",
      updated: "",
    },
  } as ISimpleFlight;
}

describe("FlightActions", () => {
  it("renders share, buy, register by default (status hidden)", () => {
    render(<FlightActions flight={makeFlight()} locale="ru" />);
    expect(screen.getByTestId("share-button")).toBeTruthy();
    expect(screen.getByTestId("buy-ticket-button")).toBeTruthy();
    expect(screen.getByTestId("registration-button")).toBeTruthy();
    expect(screen.queryByTestId("flight-status-button")).toBeNull();
  });

  it("shows status button when showStatus=true", () => {
    render(<FlightActions flight={makeFlight()} locale="ru" showStatus />);
    expect(screen.getByTestId("flight-status-button")).toBeTruthy();
  });

  it("hides share button when showShare=false", () => {
    render(<FlightActions flight={makeFlight()} locale="ru" showShare={false} />);
    expect(screen.queryByTestId("share-button")).toBeNull();
  });

  it("hides buy ticket when canBuyTicket returns false", async () => {
    const mod = await import("./visibility/buyTicketVisibility.js");
    vi.mocked(mod.canBuyTicket).mockReturnValue(false);
    render(<FlightActions flight={makeFlight()} locale="ru" />);
    expect(screen.queryByTestId("buy-ticket-button")).toBeNull();
    vi.mocked(mod.canBuyTicket).mockReturnValue(true);
  });

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