"use client";

import { DETAIL_ORGANIZER, DETAIL_AVATAR } from "@/features/opportunities/components/opportunityDetailLayout";
import { contactWhatsAppHref } from "@/lib/whatsapp";
import { externalHref } from "@/features/events/eventLifecycle";
import OptionalLink from "@/components/ui/OptionalLink";
import "@fancyapps/ui/dist/fancybox/fancybox.css";
import {
  asset,
  EventSponsors,
  } from "./eventDetailsParts";
import AssetIcon from "@/components/ui/AssetIcon";
import RemoteImage from "@/components/ui/RemoteImage";
import type {
  EventCreatorSocialKey,
  EventDetailRecord,
} from "@/features/events/eventRecord";

/**
 * The organiser card beside an event's details.
 *
 * `organizerPath` and `socialLinks` are derived on the page, not in the hook,
 * so they are explicit props — reading them off an `any` state bag would have
 * typechecked and been `undefined`.
 *
 * `event` is `EventDetailRecord` now that the detail payload is transcribed.
 * The social links are read through a key from `socialLinks`, so that tuple's
 * first element is narrowed to the five link fields the creator block actually
 * carries — a typo in it used to read `undefined` and silently drop the icon.
 */
export default function EventDetailsAside({
  event,
  organizerPath,
  socialLinks,
}: {
  event: EventDetailRecord;
  language: string;
  /* `null` when the payload carries no creator id. `profilePath` returns null
     rather than interpolating `undefined` into the URL, which is what the three
     inline copies of this rule used to produce — a link straight to a 404. */
  organizerPath: string | null;
  /* `as const` on the page, so the tuple list is readonly. The first element
     is the creator-block key to read, so it is constrained to the keys that
     exist rather than any string. */
  socialLinks: readonly (readonly [EventCreatorSocialKey, string, string])[];
}) {

  return (
  <aside className={`opportunity-detail-organizer ${DETAIL_ORGANIZER}`}>
    <div className="flex w-full flex-col items-center">
      <OptionalLink href={organizerPath}>
        <RemoteImage
          src={event.created_by?.profile_pic || asset("profile/org_profile.svg")}
          alt={event.created_by?.full_name || ""}
          width={112}
          height={112}
          className={DETAIL_AVATAR}
        />
      </OptionalLink>
      <div className="relative flex w-full flex-col items-center pt-4">
        <OptionalLink href={organizerPath} className="block w-full max-w-[252px] min-w-0">
          <div className="w-full text-center">
            <h3 className="text-base font-bold leading-relaxed text-primary-5 [overflow-wrap:anywhere]">
              {event.created_by?.full_name}
            </h3>
          </div>
        </OptionalLink>
        <div className="mt-5 flex justify-center gap-3">
          {socialLinks.map(([field, icon, label]) => {
            // A bare number / `www.` link would resolve as a path on this site.
            const raw = event.created_by?.[field];
            const href = field === "whatsapp_link" ? contactWhatsAppHref(raw) : externalHref(raw);
            return href ? (
              <a
                key={field}
                href={href}
                target="_blank"
                rel="noopener noreferrer"
                aria-label={label}
              >
                <AssetIcon src={asset(icon)} className="h-6 w-6" size={24} />
              </a>
            ) : null;
          })}
        </div>
      </div>
    </div>
    <EventSponsors sponsors={event.event_sponsor_images || []} />
  </aside>
  );
}
