import type { FC } from "react";
import { useAppSettings } from "@/shared/hooks/useAppSettings.js";
import type { ISimpleFlight } from "../../types.js";
import { AIRLINES, AIRLINES_WITH_STATUS } from "./airlines.js";
import { canBuyTicket } from "./visibility/buyTicketVisibility.js";
import { canRegister } from "./visibility/registrationVisibility.js";
import { canViewFlightStatus } from "./visibility/flightStatusVisibility.js";
import { BuyTicketButton } from "./BuyTicketButton.js";
import { RegistrationButton } from "./RegistrationButton.js";
import { FlightStatusButton } from "./FlightStatusButton.js";
import { ShareButton } from "./ShareButton.js";
import { PrintButton } from "./PrintButton.js";

export interface FlightActionsProps {
  flight: ISimpleFlight;
  locale: string;
  showStatus?: boolean;
  showPrint?: boolean;
  showShare?: boolean;
  showRegister?: boolean;
  showBuy?: boolean;
  /**
   * Skip the `canBuyTicket` status/window gate and always render the
   * Buy pill. Used by the Schedule summary header where the Buy link
   * is a generic "open the Aeroflot booking tool for this route"
   * affordance — the user can then pick any date, so gating on a
   * particular day's "Cancelled" status would hide a useful action.
   */
  forceBuy?: boolean;
}

export const FlightActions: FC<FlightActionsProps> = ({
  flight,
  locale,
  showStatus = false,
  showPrint = false,
  showShare = true,
  showRegister = true,
  showBuy = true,
  forceBuy = false,
}) => {
  const { flightStatusAvailableFromHours, buyTicketMinHours, buyTicketMaxHours } = useAppSettings();
  const now = new Date();

  const canBuy =
    showBuy && (forceBuy || canBuyTicket(flight, now, buyTicketMinHours, buyTicketMaxHours));
  const canReg = showRegister && canRegister(flight, AIRLINES);
  const canStatus =
    showStatus &&
    canViewFlightStatus(flight, now, flightStatusAvailableFromHours, AIRLINES_WITH_STATUS);

  const shareUrl = typeof window !== "undefined" ? window.location.href : "";

  return (
    <div className="flight-actions" data-testid="flight-actions">
      {showPrint && <PrintButton flight={flight} />}
      {showShare && <ShareButton url={shareUrl} locale={locale} />}
      {canBuy && <BuyTicketButton flight={flight} locale={locale} />}
      {canReg && <RegistrationButton flight={flight} />}
      {canStatus && <FlightStatusButton flight={flight} locale={locale} />}
    </div>
  );
};
