"use client";

import { useTranslation } from "react-i18next";
import { SOCIAL_LINKS } from "./learnServeDetailsTypes";
import type { LearnServeOpportunityData } from "./learnServeDetailsTypes";
import RemoteImage from "@/components/ui/RemoteImage";
import { contactWhatsAppHref } from "@/lib/whatsapp";
import { externalHref } from "@/features/events/eventLifecycle";

/**
 * How to reach the organizer: the opportunity's own WhatsApp number, then their
 * profile's social links.
 */
export default function OrganizerContactLinks({
  opportunityData,
}: {
  opportunityData?: LearnServeOpportunityData | null;
  isCreator: boolean;
}) {
  const { t } = useTranslation();

  return (
    <div className="w-full pt-2 text-center">
      {SOCIAL_LINKS.some(({ key }) => key === "whatsapp_link" ? opportunityData?.whatsapp_link || opportunityData?.created_by?.[key] : opportunityData?.created_by?.[key]) && (
        <div>
          <div className="mt-3 flex gap-3 justify-center">
            {SOCIAL_LINKS.map(({ key, icon }) => {
              const raw = key === "whatsapp_link"
                ? opportunityData?.whatsapp_link || opportunityData?.created_by?.[key]
                : opportunityData?.created_by?.[key];
              // A profile's WhatsApp may be a bare number (`string|max:15`),
              // which as a raw href would resolve as a path on this site.
              // Other profiles links: http(s) only, `https://` added when missing.
              const href = key === "whatsapp_link" ? contactWhatsAppHref(raw) : raw && externalHref(raw);
              if (!href) return null;
              return (
                <a key={key} target="_blank" rel="noopener noreferrer" href={href} aria-label={t(`COMMON.SOCIAL_${key.replace("_link", "").toUpperCase()}`)}>
                  <RemoteImage
  src={icon}
  alt={""}
  width={31}
  height={31}
  className="h-6 w-6"
/>
                </a>
              );
            })}
          </div>
        </div>
      )}
    </div>
  );
}
