"use client";

import { useTranslation } from "react-i18next";
import { FiInfo } from "react-icons/fi";
import AssetIcon from "@/components/ui/AssetIcon";

/**
 * The badge strip overlaid on an opportunity thumbnail, and its public/private
 * indicator. Shared by the three card components and the detail screens so the
 * emergency badge and the visibility tooltip only exist in one place.
 *
 * `is_relief` is the "Outside Kuwait" classification and `is_emergency` is the
 * separate emergency priority — two independent flags that happen to render
 * next to each other.
 */
export interface OpportunityBadgeFlags {
  is_urgent?: boolean | null;
  is_supports_disabled?: boolean | null;
  is_relief?: boolean | null;
  is_emergency?: boolean | null;
}

export function OpportunityBadges({
  item,
  className = "",
}: {
  item: OpportunityBadgeFlags;
  className?: string;
}) {
  const { t } = useTranslation();

  const hasAnyBadge =
    item.is_urgent ||
    item.is_supports_disabled ||
    item.is_relief ||
    item.is_emergency;

  if (!hasAnyBadge) return null;

  return (
    <div className={`absolute top-0 right-0 pr-4 pt-4 flex flex-col items-end gap-2 ${className}`} >
      {(item.is_emergency || item.is_urgent) && (
        <div className="w-8 h-8">
          {/* The three badges are painted at 32x32 by the wrapper (`w-8 h-8`)
              plus `w-full h-full`; the numbers passed here are each SVG's own
              `width`/`height`, which is what reserves the right aspect box
              before it loads. `alt` is a real label on all three — the badge
              carries information the card does not repeat in text. */}
          <AssetIcon
            src="/assets/voluneteerevent/urgent.svg"
            alt={t(item.is_emergency ? "COMMON.EMERGENCY_PRIORITY" : "COMMON.URGENT")}
            size={20}
            className="w-full h-full"
          />
        </div>
      )}
      {item.is_supports_disabled && (
        <div className="w-8 h-8">
          <AssetIcon
            src="/assets/voluneteerevent/person_disability_card.svg"
            alt={t("COMMON.SUPPORTS_DISABLED")}
            size={65}
            className="w-full h-full"
          />
        </div>
      )}
      {/* The tile's `#3b3375` is sampled from the artwork the client
          supplied, so the square reads as one piece with the glyph rather
          than as a blue chip behind it. Deliberately not `primary-5`
          (`#29246D`): close, but a visibly darker purple than the asset. */}
      {item.is_relief && (
        <div className="w-8 h-8 bg-[#3b3375] p-1 rounded-md flex items-center justify-center">
          {/* 938x914 — not square, so both are given. */}
          <AssetIcon
            src="/assets/voluneteerevent/airplane_logo_vector.svg"
            alt={t("COMMON.RELIEF")}
            width={938}
            height={914}
            className="w-full h-full"
          />
        </div>
      )}
    </div>
  );
}

/**
 * The "i" affordance sitting beside the opportunity type, explaining whether the
 * opportunity is open to everyone (`is_public: true`) or invitation-only.
 *
 * `title` carries the explanation so it works on hover, on focus and for screen
 * readers without pulling in a tooltip library — touch devices get the label
 * text next to the icon instead.
 */
export function OpportunityVisibilityInfo({
  isPublic,
  showLabel = false,
  className = "",
}: {
  isPublic?: boolean | null;
  showLabel?: boolean;
  className?: string;
}) {
  const { t } = useTranslation();

  // `undefined` means the payload predates the field — say nothing rather than
  // guessing a visibility.
  if (isPublic == null) return null;

  const label = t(
    isPublic ? "COMMON.PUBLIC_OPPORTUNITY" : "COMMON.PRIVATE_OPPORTUNITY"
  );
  const hint = t(
    isPublic
      ? "COMMON.PUBLIC_OPPORTUNITY_HINT"
      : "COMMON.PRIVATE_OPPORTUNITY_HINT"
  );

  return (
    <span
      className={`inline-flex items-center gap-1 align-middle ${className}`}
      title={`${label} — ${hint}`}
    >
      <FiInfo
        className="w-4 h-4 shrink-0 text-primary-5"
        aria-hidden="true"
        focusable="false"
      />
      <span className={showLabel ? "text-xs leading-tight" : "sr-only"}>
        {label}
      </span>
    </span>
  );
}

export default OpportunityBadges;
