"use client";

import Link from "next/link";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/Button";
import { getDefaultProfileImage } from "@/lib/helpers";
import OpportunitySponsors from "./OpportunitySponsors";
import OrganizerContactLinks from "./OrganizerContactLinks";
import type { LearnServeOpportunityData } from "./learnServeDetailsTypes";
import RemoteImage from "@/components/ui/RemoteImage";

import { DETAIL_ORGANIZER, DETAIL_AVATAR } from "./opportunityDetailLayout";

const ROW_BUTTON_CLASS = "!w-full max-w-[220px] px-5 text-sm font-bold !rounded-full";

interface LearnServeOrganizerColumnProps {
  opportunityData?: LearnServeOpportunityData | null;
  /** Where the organizer's own profile lives — public or private. */
  organizerPath: string;
  isCreator: boolean;

  /** The organizer's printable attendance code, and the participant's scanner. */
  canIssueAttendanceCode: boolean;
  handleIssueAttendanceCode: () => void;

  goToRegisteredList: () => void;
}

/**
 * The left column of the development-opportunity page: who is running it, how
 * to reach them, and the organizer's own controls.
 */
export default function LearnServeOrganizerColumn({
  opportunityData,
  organizerPath,
  isCreator,
  canIssueAttendanceCode,
  handleIssueAttendanceCode,
  goToRegisteredList,
}: LearnServeOrganizerColumnProps) {
  const { t } = useTranslation();

  return (
    <div className={`opportunity-detail-organizer ${DETAIL_ORGANIZER}`}>
      <Link href={organizerPath}>
        <RemoteImage
          src={opportunityData?.created_by?.profile_pic || getDefaultProfileImage(
            opportunityData?.created_by?.gender_display?.value_en,
            "/assets/profile/male_profile.svg",
            "/assets/profile/female_profile.svg",
            "/assets/profile/org_profile.svg"
          )}
          alt={opportunityData?.created_by?.full_name ?? ""}
          width={112}
          height={112}
          className={DETAIL_AVATAR}
        />
      </Link>
      <Link href={organizerPath} className="mt-4 block w-full min-w-0 max-w-[220px] text-center">
        <h3 className="text-base font-bold leading-relaxed text-primary-5 [overflow-wrap:anywhere]">
          {opportunityData?.created_by?.full_name}
        </h3>
      </Link>
      <OrganizerContactLinks opportunityData={opportunityData} isCreator={isCreator} />

      {Boolean(opportunityData?.opportunity_sponsor_images?.length) && (
        <div className="sponseritm relative my-5 w-full rounded-3xl bg-[#F7F7FB] px-3 py-5 [&_h2]:mb-4 [&_h2]:text-base">
          <OpportunitySponsors sponsors={opportunityData?.opportunity_sponsor_images?.map(image => ({
            id: Number(image.id), image: image.image, organization: image.organization, position: image.position,
          })) || []} />
        </div>
      )}
      {isCreator && (
        <div className="flex w-full flex-col items-center gap-3 pt-5">
          <Button variant="primary" size="medium" onClick={goToRegisteredList} className={ROW_BUTTON_CLASS}>
            {t("COMMON.PARTICIPANTS_LIST")}
          </Button>
          <Button variant="primary" size="medium" onClick={handleIssueAttendanceCode}
            disabled={!canIssueAttendanceCode} className={ROW_BUTTON_CLASS}>
            {t("COMMON.ATTENDANCE_QR")}
          </Button>
        </div>
      )}
    </div>
  );
}
