"use client";

import { useTranslation } from "react-i18next";
import { useAuthStore } from "@/store/authStore";
import { isViewerOrganizer } from "@/features/shared/opportunityButtonState";
import LocationLink from "@/components/ui/LocationLink";
import { externalHref } from "@/features/events/eventLifecycle";
import {
  opportunityNationalityLabel,
  opportunityNationalityFrom,
} from "@/data/Constants";
import { CERTIFICATE_TYPES } from "./learnServeDetailsTypes";
import type { LearnServeOpportunityData } from "./learnServeDetailsTypes";
import AssetIcon from "@/components/ui/AssetIcon";
import RemoteImage from "@/components/ui/RemoteImage";

interface LearnServeAudienceTermsProps {
  opportunityData?: LearnServeOpportunityData | null;
  selectedLanguage: string;
  /** In person rather than online — decides whether an address is shown. */
  isInPerson: boolean;
  isCreator: boolean;
}

/**
 * The right-hand column of the terms block: where it happens, its certificate,
 * and who may attend.
 */
export default function LearnServeAudienceTerms({
  opportunityData,
  selectedLanguage,
  isInPerson,
  isCreator,
}: LearnServeAudienceTermsProps) {
  const { t } = useTranslation();
  const viewerId = useAuthStore((state) => state.user?.id);
  const canViewLink = isCreator || isViewerOrganizer(opportunityData, viewerId) || opportunityData?.is_registered === true;

  return (
    <div className="w-1/2 xss:w-full">
      <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
        <RemoteImage
  src={
            isInPerson
              ? "/assets/voluneteerevent/inperson.svg"
              : "/assets/voluneteerevent/online.svg"
          }
  alt=""
  width={20}
  height={20}
  className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`}
/>
        <p className="text-secondary-102 2xl:text-xl lg:text-base text-base font-bold">
          {opportunityData?.format_display
            ? selectedLanguage === "ar"
              ? opportunityData.format_display.value_ar
              : opportunityData.format_display.value_en
            : t("COMMON.ONLINE")}
        </p>
      </div>

      <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
        {isInPerson ? (
          <>
            <AssetIcon src="/assets/homepage/locations.svg" alt=" Location Icon" className={`${selectedLanguage === "ar"
                  ? "ml-3 right-[3px]"
                  : "mr-3 left-[3px]"
                } w-5 h-5 object-contain relative`} size={20} />
            <div>
              <LocationLink
                // New records carry only a map link (no text location, p.18):
                // the pin label or "open the link" reads better than "not found".
                address={
                  (selectedLanguage === "ar"
                    ? opportunityData?.location_ar
                    : opportunityData?.location_en) ||
                  opportunityData?.map_desc ||
                  (opportunityData?.location_url
                    ? t("COMMON.OPEN_LOCATION_LINK")
                    : t("COMMON.ADDRESS_NOT_FOUND"))
                }
                locationUrl={opportunityData?.location_url}
                latitude={opportunityData?.latitude}
                longitude={opportunityData?.longitude}
                className="text-secondary-102 2xl:text-xl lg:text-base text-base font-bold line-clamp-1 cursor-pointer hover:underline"
              />
            </div>
          </>
        ) : (
          <>
            <AssetIcon src="/assets/voluneteerevent/onlinelink.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
            <div className="flex gap-3 items-center">
              {/* The meeting link is only revealed to the organizer
                  and to registered participants. */}
              {canViewLink ? (
                <a
                  href={externalHref(opportunityData?.link) ?? "#"}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="text-secondary-102 2xl:text-xl lg:text-base text-base font-bold cursor-pointer hover:underline"
                >
                  {t("COMMON.ONLINELINK")}
                </a>
              ) : (
                <>
                  <p className="text-secondary-102 2xl:text-xl lg:text-base text-base font-bold">
                    {t("COMMON.ONLINELINK")}
                  </p>
                  <div className="relative inline-block group">
                    {/* 20x20 intrinsic. The tooltip is positioned against
                        this element, so reserving its box also stops the
                        tooltip anchoring to a zero-width node on first paint. */}
                    <AssetIcon
                      src="/assets/voluneteerevent/linkinfo.svg"
                      size={20}
                      className="cursor-pointer"
                    />
                    <div className="absolute left-full top-1/2 transform -translate-y-1/2 ml-2 bg-white rounded-lg shadow-lg opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all duration-200 w-[200px] z-50 p-3 xsl:w-[220px] xss:w-[150px] xsl:left-auto xsl:right-[100%] xss:left-[100%] xss:right-auto">
                      {t("COMMON.LINK_AFTER_REGISTRATION")}
                    </div>
                  </div>
                </>
              )}
            </div>
          </>
        )}
      </div>

      {CERTIFICATE_TYPES.includes(
        opportunityData?.learning_type_display?.value_en || ""
      ) && (
          <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
            <AssetIcon src="/assets/voluneteerevent/certificate.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
            <p className="2xl:text-xl lg:text-base text-base font-bold text-primary-5">
              {opportunityData?.certificate_type_display
                ? selectedLanguage === "ar"
                  ? opportunityData.certificate_type_display.value_ar
                  : opportunityData.certificate_type_display.value_en
                : t("COMMON.CERTIFICATE_ATTENDANCE")}
            </p>
          </div>
        )}

      <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
        <AssetIcon src="/assets/voluneteerevent/nationality.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
        <p className="2xl:text-xl lg:text-base text-base font-bold text-primary-5">
          {opportunityNationalityLabel(
            opportunityNationalityFrom(opportunityData ?? {}),
            selectedLanguage
          )}
        </p>
      </div>
    </div>
  );
}
