"use client";

import Image from "next/image";
import type { TableColumn } from "@/components/ui/Table";
import { getDefaultProfileImage } from "@/lib/helpers";
import { registrationPerson } from "@/features/opportunities/registrationRow";
import RegistrationEmergencyCell from "./RegistrationEmergencyCell";
import { type RegisteredUser } from "./learnServeRegisterListTypes";
import ProfileLink from "@/components/shared/ProfileLink";

const asset = (path: string) => `/assets/${path}`;

/**
 * One cell of the read-only registrations table.
 *
 * The attendance list's cells live in `RegistrationRowCells`; they are kept
 * apart because that table has an attendance checkbox and a delete action this
 * one does not, and this one keys its name column on `user_full_name` rather
 * than `full_name`.
 */
export default function RegisterListRowCells({
  column,
  rowData,
  selectedLanguage,
}: {
  column: TableColumn;
  rowData: RegisteredUser;
  selectedLanguage: string;
}) {
  const person = registrationPerson(rowData);

  if (column.key === "user_full_name") {
    return (
      <div className="flex items-center gap-3">
        <Image
          src={
            person.profilePic ||
            getDefaultProfileImage(
              person.genderEn,
              asset("profile/male_profile.svg"),
              asset("profile/female_profile.svg"),
              asset("profile/org_profile.svg")
            )
          }
          alt={person.name}
          width={40}
          height={40}
          unoptimized
          className="w-10 h-10 rounded-full object-cover"
        />
        <div className="flex flex-col">
          <ProfileLink
            id={person.userId}
            className="cursor-pointer text-primary-5"
          >
            {person.name || "-"}
          </ProfileLink>
          {person.civilId && (
            <span className="text-xs text-gray-500">{person.civilId}</span>
          )}
        </div>
      </div>
    );
  }
  if (column.type === "email") return <span>{person.email || "-"}</span>;
  if (column.type === "contact") return <bdi dir="ltr" className="inline-block whitespace-nowrap">{person.phone || "-"}</bdi>;
  if (column.type === "emergency") {
    return (
      <RegistrationEmergencyCell
        columnKey={column.key}
        rowData={rowData}
        selectedLanguage={selectedLanguage}
      />
    );
  }
  return (
    <span>{String((rowData as unknown as Record<string, unknown>)[column.key] ?? "")}</span>
  );
}
