"use client";

import { useState } from "react";
import Image from "next/image";
import Link from "next/link";
import { getDefaultProfileImage } from "@/lib/helpers";
import { type UserProfile } from "./MoreProfile";
import { directoryDisplayName } from "../profileDirectoryName";

const femaleDummy = "/assets/profile/female_profile.svg";
const orgDummy = "/assets/profile/org_profile.svg";
const maleDummy = "/assets/profile/male_profile.svg";

/**
 * One profile tile in the directory grid.
 *
 * Moved out of `MoreProfile.tsx`, where it was a second exported component in
 * the same file. Nothing imports it by name today, but the export is kept — and
 * re-exported from `MoreProfile` — so the move cannot break a caller.
 */
export const ProfileCard = ({ profile }: { profile: UserProfile }) => {
  const userType = profile?.user_details?.user_type;
  const profileUrl = `/public-profile/${profile?.user_details?.id}`;

  /*
   * An individual volunteer is identified by nickname only — their real name
   * stays private, so there is deliberately no fall back to first/last name
   * here. That matches `/public-profile/{id}`, where the API returns
   * `full_name: null` for a volunteer for the same reason, and the header shows
   * `display_name || nickname || "—"`.
   *
   * Organizations and volunteer teams keep the name fallback: their name is
   * their public identity, not personal information.
   */
  const isIndividual = userType === "volunteer" || userType === "individual";
  // BE-169: a volunteer row carries `display_name` — the real name when they
  // chose to show it, else the nickname. BE-294: an organization / team row
  // carries its registered name as `display_name` / `company_name`; the
  // nickname is only the seeded username. See `directoryDisplayName`.
  const displayName = directoryDisplayName(profile, isIndividual);
  // A third-party avatar (a Google account picture) can fail to load or be refused; the tile
  // then shows the default avatar instead of a broken-image icon.
  const [pictureFailed, setPictureFailed] = useState(false);
  const defaultPicture = getDefaultProfileImage(
    profile?.user_details?.gender_display?.value_en,
    maleDummy,
    femaleDummy,
    orgDummy
  );

  return (
    <div className="flex flex-col items-center">
      <div className="flex justify-center items-center w-full h-full">
        <Link href={profileUrl}>
          <div className="relative lg:w-48 lg:h-48 md:w-32 md:h-32 mobilescreen:w-24 mobilescreen:h-24">
            <Image
              className="rounded-full cursor-pointer border-2 border-primary-5 hover:opacity-90 transition-opacity object-cover"
              src={(!pictureFailed && profile?.user_details?.profile_pic) || defaultPicture}
              onError={() => setPictureFailed(true)}
              referrerPolicy="no-referrer"
              // Same rule as the heading — the alt text is rendered HTML and
              // would leak the real name just as readily.
              alt={displayName}
              fill
              unoptimized
            />
          </div>
        </Link>
      </div>
      <h3 className="text-center font-semibold 2xl:text-[25px] lg:text-lg mediumscreen:text-xl mobilescreen:text-sm text-lg text-secondary-100 pt-2">
        <Link href={profileUrl} className="cursor-pointer hover:underline">
          {displayName}
        </Link>
      </h3>
    </div>
  );
};
