"use client";

import Image from "next/image";
import Link from "next/link";
import { useTranslation } from "react-i18next";
import {
  ChevronDownIcon,
  ChevronUpIcon,
} from "./headerIcons";
import AccountSwitcher from "./AccountSwitcher";
import { DropdownItem } from "./headerParts";
import { clickable } from "@/lib/a11y";

/**
 * The profile entry in the desktop nav, with its account dropdown.
 *
 * Which destinations appear depends on whether the viewer is a volunteer or
 * an organisation, so the list is built here rather than being a constant.
 */
export default function HeaderProfileDropdown({
  isVolunteerUser,
  profilePicUrl,
  defaultProfilePic,
  dropdownOpen,
  setDropdownOpen,
  close,
  handleLogout,
  handleLogoutAll,
  hasOtherAccounts,
}: {
  isVolunteerUser: boolean;
  pathname: string;
  profilePicUrl: string;
  /** Shown when the account's own picture is missing or fails to load. */
  defaultProfilePic: string;
  dropdownOpen: string | null;
  setDropdownOpen: React.Dispatch<React.SetStateAction<string | null>>;
  close: () => void;
  handleLogout: () => void;
  /** BE-129 — shown only while a second session exists. */
  handleLogoutAll: () => void;
  hasOtherAccounts: boolean;
}) {
  const { t } = useTranslation();

  return (

    <li className="flex text-primary-5 relative">
      <span className="text-primary-5 2xl:text-lg lg:text-base sm:text-base flex gap-3 items-center">
        <Link
          href={isVolunteerUser ? "/volunteer-profile" : "/entities-profile"}
          onClick={close}
          aria-label={t("COMMON.VOLUNTEER.PROFILE") || "Profile"}
        >
          <Image
            src={profilePicUrl}
            alt=""
            width={48}
            height={48}
            className="w-12 h-12 object-cover rounded-full border border-primary-5"
            onError={(e) => {
              (e.target as HTMLImageElement).src = defaultProfilePic;
            }}
          />
        </Link>
        <button
          className="cursor-pointer focus:outline-none"
          aria-label={t("COMMON.TOGGLE_PROFILE_MENU") || "Toggle profile menu"}
          aria-expanded={dropdownOpen === "profile"}
          onClick={() =>
            setDropdownOpen(dropdownOpen === "profile" ? null : "profile")
          }
        >
          {dropdownOpen === "profile" ? (
            <ChevronUpIcon className="text-primary-5 font-bold text-[30px]" aria-hidden="true" />
          ) : (
            <ChevronDownIcon className="text-primary-5 font-bold text-[30px]" aria-hidden="true" />
          )}
        </button>
      </span>
      {/*
        The menu is anchored left in both directions now that the account
        group is pinned to the left of the header. It used to be `right-0` in
        LTR, which was correct while the avatar sat at the far right — the
        menu opened inwards. From the left it would open outwards instead and
        a 245px panel would run off the edge of the viewport. RTL already
        anchored left for exactly this reason; the two cases have simply
        become the same case.
      */}
      {dropdownOpen === "profile" && (
        <ul className="absolute cursor-pointer left-0 mt-2 bg-white shadow-[0px_4px_8px_3px_#00000026] rounded-[15px] py-2 top-[75px] laptop:top-[58px] lg:top-[60px] w-[245px] rtl:w-[260px] dropdown-menu z-50">
          {isVolunteerUser ? (
            <>
              {/* <DropdownItem
              close={close}
                href="/account-information"
                icon="/assets/auth/accountsetting.svg"
                label={t("COMMON.ACCOUNT.SETTINGS")}
              /> */}
              <DropdownItem
              close={close}
                href="/volunteer-profile"
                icon="/assets/auth/volunteerprofile.svg"
                label={t("COMMON.VOLUNTEER.PROFILE")}
              />
              {/* <DropdownItem
              close={close}
                href="/volunteer-qr-code"
                icon="/assets/auth/qrcode.svg"
                label={t("COMMON.QRCODE")}
              /> */}
              {/* <DropdownItem
              close={close}
                href="/calendar"
                icon="/assets/auth/mycalander.svg"
                label={t("COMMON.MY.CALENDAR")}
              /> */}
              <DropdownItem
              close={close}
                href="/achievement-reports"
                icon="/assets/auth/file-text.svg"
                label={t("COMMON.MY_REPORT")}
              />
            </>
          ) : (
            <>
              {/* <DropdownItem
              close={close}
                href="/account-information"
                icon="/assets/auth/accountsetting.svg"
                label={t("COMMON.ACCOUNT.SETTINGS")}
              /> */}
              <DropdownItem
              close={close}
                href="/entities-profile"
                icon="/assets/auth/volunteerprofile.svg"
                label={t("COMMON.ORGANIZATION.PROFILE")}
              />
              {/* <DropdownItem
              close={close}
                href="/calendar"
                icon="/assets/auth/mycalander.svg"
                label={t("COMMON.MY.CALENDAR")}
              /> */}
            </>
          )}
          {/* BE-129 — who you are now, who else is signed in here, and the
              way into one more. Above sign-out, which reads differently
              depending on what this list holds. */}
          {!isVolunteerUser && <DropdownItem close={close} href="/achievement-reports" icon="/assets/auth/file-text.svg" label={t("COMMON.MY_REPORT")} />}
          <AccountSwitcher close={close} />

          <li
            className="px-4 py-2 hover:bg-[#F5F5F5] mx-2 rounded-xl flex items-center gap-3 h-12 whitespace-nowrap text-base font-bold text-secondary-101 cursor-pointer"
            /* Logging out was mouse-only; the other items in this menu are
               `<Link>`s and were already reachable. */
            {...clickable(handleLogout)}
          >
            <Image
              src="/assets/auth/logout.svg"
              alt=""
              width={20}
              height={20}
              className="w-5 h-5 shrink-0 object-contain"
              unoptimized
            />
            {t("COMMON.LOGOUT")}
          </li>

          {/* Offered only when it means something different from the item
              above it. */}
          {hasOtherAccounts && (
            <li
              className="px-4 py-2 hover:bg-[#F5F5F5] mx-2 rounded-xl flex items-center gap-3 h-12 whitespace-nowrap text-base font-bold text-secondary-102 cursor-pointer"
              {...clickable(handleLogoutAll)}
            >
              <Image
                src="/assets/auth/logout.svg"
                alt=""
                width={20}
                height={20}
                className="w-5 h-5 shrink-0 object-contain"
                unoptimized
              />
              {t("COMMON.LOGOUT_ALL")}
            </li>
          )}
        </ul>
      )}
    </li>
  );
}
