"use client";

import Image from "next/image";
import { useTranslation } from "react-i18next";
import { Check } from "lucide-react";

import {
  accountKindKey,
  accountLabel,
  type AccountSummary,
} from "@/features/auth/accountLabel";
import { getDefaultProfileImage } from "@/lib/helpers";

/**
 * BE-129 — one account in the switcher: avatar, name, and what kind of account
 * it is.
 *
 * The current one is a row too, marked and not pressable. Showing it is the
 * point rather than decoration: with two accounts open the first question is
 * always "which one am I right now", and a menu that lists only the *others*
 * answers it by implication at best.
 */
export default function AccountRow({
  account,
  profilePic,
  isCurrent = false,
  onSelect,
}: {
  account: AccountSummary;
  profilePic?: string | null;
  isCurrent?: boolean;
  onSelect?: () => void;
}) {
  const { t } = useTranslation();
  const name = accountLabel(account);
  const kindKey = accountKindKey(account);
  /* Same silhouette set as the header avatar and every list row in the app.
     An organization has no `gender_display`, so the fourth argument serves it
     without a branch — as does a volunteer whose gender is not known yet. */
  const fallbackPic = getDefaultProfileImage(
    account.gender_display?.value_en,
    "/assets/profile/male_profile.svg",
    "/assets/profile/female_profile.svg",
    "/assets/profile/org_profile.svg"
  );

  const body = (
    <>
      <Image
        src={profilePic || fallbackPic}
        alt=""
        width={32}
        height={32}
        className="h-8 w-8 shrink-0 rounded-full border border-primary-5 object-cover"
        unoptimized
        onError={(event) => {
          (event.target as HTMLImageElement).src = fallbackPic;
        }}
      />
      <span className="min-w-0 flex-1 text-start">
        <span className="block truncate font-bold text-secondary-101">{name}</span>
        {/* {kindKey && (
          <span className="block truncate text-xs font-normal text-secondary-102">
            {t(kindKey)}
          </span>
        )} */}
      </span>
      {isCurrent && (
        <Check className="h-4 w-4 shrink-0 text-primary-504" aria-hidden="true" />
      )}
    </>
  );

  if (isCurrent) {
    return (
      <li
        className="mx-2 flex items-center gap-3 rounded-xl bg-[#F5F5F5] px-4 py-2"
        aria-current="true"
        aria-label={`${t("COMMON.CURRENT_ACCOUNT")}: ${name}`}
      >
        {body}
      </li>
    );
  }

  return (
    <li className="mx-2 rounded-xl hover:bg-[#F5F5F5]">
      <button
        type="button"
        onClick={onSelect}
        className="flex w-full items-center gap-3 px-4 py-2 text-base"
        aria-label={t("COMMON.SWITCH_TO_ACCOUNT", { name })}
      >
        {body}
      </button>
    </li>
  );
}
