/**
 * TZ §4.1.10.1: when a flight-number search returns only Pobeda ("DP")
 * and/or Aurora ("HZ") flights, show a redirect banner with links to
 * the subsidiary carrier websites instead of the normal flight list.
 *
 * Render only in "flight" search mode with non-empty results where every
 * flight's operating carrier is DP or HZ.
 */

import type { FC } from "react";
import { useTranslation } from "@/i18n/provider.js";
import { operatingCarrier } from "../types.js";
import type { ISimpleFlight } from "../types.js";
import { resolveCarrierByFlightNumber } from "@/shared/operatorIcon.js";
import "./PobedaAuroraBanner.scss";

const POBEDA_URL = "https://www.pobeda.aero";
const AURORA_URL = "https://www.flyaurora.ru";

export function shouldShowPobedaAuroraBanner(
  flights: readonly ISimpleFlight[],
): { show: boolean; hasPobeda: boolean; hasAurora: boolean } {
  if (flights.length === 0) return { show: false, hasPobeda: false, hasAurora: false };
  let hasPobeda = false;
  let hasAurora = false;
  for (const f of flights) {
    const carrier =
      operatingCarrier(f.operatingBy) ??
      (f.flightId.carrier === "SU"
        ? resolveCarrierByFlightNumber(f.flightId.flightNumber)
        : f.flightId.carrier);
    if (carrier === "DP") hasPobeda = true;
    else if (carrier === "HZ") hasAurora = true;
    else return { show: false, hasPobeda: false, hasAurora: false };
  }
  return { show: hasPobeda || hasAurora, hasPobeda, hasAurora };
}

export interface PobedaAuroraBannerProps {
  hasPobeda: boolean;
  hasAurora: boolean;
}

export const PobedaAuroraBanner: FC<PobedaAuroraBannerProps> = ({
  hasPobeda,
  hasAurora,
}) => {
  const { t } = useTranslation();
  return (
    <section
      className="pobeda-aurora-banner"
      data-testid="pobeda-aurora-banner"
      role="status"
    >
      <h3 className="pobeda-aurora-banner__title">
        {t("BOARD.POBEDA-AURORA-TITLE")}
      </h3>
      <p className="pobeda-aurora-banner__body">
        {t("BOARD.POBEDA-AURORA-BODY")}
      </p>
      <ul className="pobeda-aurora-banner__links">
        {hasPobeda && (
          <li>
            <a href={POBEDA_URL} target="_blank" rel="noopener noreferrer">
              {t("BOARD.POBEDA-AURORA-LINK-POBEDA")}
            </a>
          </li>
        )}
        {hasAurora && (
          <li>
            <a href={AURORA_URL} target="_blank" rel="noopener noreferrer">
              {t("BOARD.POBEDA-AURORA-LINK-AURORA")}
            </a>
          </li>
        )}
      </ul>
    </section>
  );
};
