"use client";

import { useTranslation } from "react-i18next";

import { getDefaultProfileImage } from "@/lib/helpers";
import {
  registrationPerson,
  type RegistrationRow,
} from "@/features/opportunities/registrationRow";
import ProfileLink from "@/components/shared/ProfileLink";
import RemoteImage from "@/components/ui/RemoteImage";

/**
 * The volunteer's avatar, name and civil ID, in the volunteer-registrations
 * table.
 *
 * Read through `registrationPerson()`, like the two register-list screens,
 * rather than reaching into the row. It was reading `profile_pic`,
 * `gender_display` and `is_public` **only** off a nested `user` object — and
 * neither registration resource puts them there:
 *
 * - `VolunteerOpportunityRegistrationResource`'s nested `user` holds exactly
 *   `id` plus the four emergency-contact fields, and the three keys above are
 *   absent from the row entirely. That is BE-103, a backend gap, so this cell
 *   currently falls back for every row no matter where it looks.
 * - `LearnServeOpportunityRegistrationResource` sends all three **flat**.
 *
 * So the old reads were harmless *today* and would have stayed broken the day
 * BE-103 is fixed, because the suggested fix adds the fields flat — which is
 * exactly the shape this cell was not looking at. The adapter reads flat first
 * and nested second, so it is right under both.
 */
export default function RegistrationNameCell({
  rowData,
  missedCheckOut = false,
}: {
  rowData: RegistrationRow;
  /** Checked in on the shown day but never checked out (hours 0:00). */
  missedCheckOut?: boolean;
}) {
  const { t } = useTranslation();
  const person = registrationPerson(rowData);

  return (
    <div className="flex items-center gap-3">
      <RemoteImage
        src={
          person.profilePic ||
          getDefaultProfileImage(
            person.genderEn,
            "/assets/profile/male_profile.svg",
            "/assets/profile/female_profile.svg",
            "/assets/profile/org_profile.svg"
          )
        }
        alt={person.name}
        width={40}
        height={40}
        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>
        {missedCheckOut && (
          <span className="mt-1 w-fit rounded-full bg-amber-100 px-2 py-0.5 text-xs font-bold text-amber-900">
            {t("COMMON.MISSED_CHECK_OUT")}
          </span>
        )}
        {/* Reports show the volunteer's civil ID under the name */}
        {person.civilId && (
          <span className="text-xs text-gray-500">{person.civilId}</span>
        )}
      </div>
    </div>
  );
}
