"use client";

import Image from "next/image";
import Link from "next/link";
import { useTranslation } from "react-i18next";
import { ChevronDownIcon, ChevronUpIcon } from "./headerIcons";
import MobileAccountActions from "./MobileAccountActions";
import { moreNavLinks, primaryNavLinks } from "./headerNavLinks";

export default function HeaderMobileMenu({
  pathname,
  isLoggedIn,
  isVolunteer,
  close,
  handleLogout,
  handleLogoutAll,
  dropdownOpen,
  setDropdownOpen,
  profilePicUrl,
  defaultProfilePic,
}: {
  pathname: string;
  isLoggedIn: boolean;
  isVolunteer: boolean;
  close: () => void;
  handleLogout: () => void;
  /** BE-129 — sign out of every account; see `useHeader`. */
  handleLogoutAll: () => void;
  dropdownOpen: string | null;
  setDropdownOpen: React.Dispatch<React.SetStateAction<string | null>>;
  profilePicUrl: string;
  /** Used when the account's own picture is missing or fails to load. */
  defaultProfilePic: string;
  /* Declared but never destructured — the badge lives in `Header` itself. */
  unreadCount: number;
}) {
  const { t } = useTranslation();

  const renderPrimaryLink = ({ href, label }: { href: string; label: string }) => (
    <li key={href} className={`border-b text-primary-5 text-sm ${pathname === href ? "font-bold" : ""}`}>
      <Link href={href} className="px-4 py-3 block w-full" onClick={close}>
        {label}
      </Link>
    </li>
  );

  return (
      <ul className="mediumscreen:hidden bg-white shadow-md mobile-menu">
        {/* Same order as the desktop nav (`HeaderDesktopNav`): Home, then the
            Opportunities dropdown, then the rest. The phone menu used to list
            all four primary links first and push Opportunities to fifth. */}
        {primaryNavLinks(t).slice(0, 1).map(renderPrimaryLink)}

        {/* Opportunities sub-menu. The whole row toggles it — a target the
            size of the chevron was too small on a phone. The chevron sits right
            after the label, as on desktop; `flex-1` used to push it to the far
            edge of the row (the left edge in Arabic), away from its label. The row used to
            link to /opportunities as well, so that page is the first item. */}
        <li className="border-b text-primary-5 text-sm">
          <button
            type="button"
            aria-expanded={dropdownOpen === "opportunities"}
            onClick={() => setDropdownOpen(dropdownOpen === "opportunities" ? null : "opportunities")}
            className="px-4 py-3 flex w-full gap-1 items-center text-start focus:outline-none"
          >
            <span>{t("COMMON.OPPORTUNITIES")}</span>
            {dropdownOpen === "opportunities" ? (
              <ChevronUpIcon className="text-primary-5 font-bold text-[20px]" aria-hidden="true" />
            ) : (
              <ChevronDownIcon className="text-primary-5 font-bold text-[20px]" aria-hidden="true" />
            )}
          </button>
        </li>
        {dropdownOpen === "opportunities" && (
          <ul className="bg-gray-100">
            <li className="border border-b-primary-5/40 text-primary-5">
              <Link href="/volunteer-opportunities-list" className="w-full block text-sm ps-5 py-2 pe-6" onClick={close}>{t("COMMON.VOLUNTEER")}</Link>
            </li>
            <li className="border border-b-primary-5/40 text-primary-5">
              <Link href="/development-opportunities-list" className="w-full block text-sm ps-5 py-2 pe-6" onClick={close}>{t("COMMON.LEARN_SHARE")}</Link>
            </li>
          </ul>
        )}

        {primaryNavLinks(t).slice(1).map(renderPrimaryLink)}

        {/* More sub-menu — the whole row toggles it. */}
        <li className="border-b text-primary-5 text-sm">
          <button
            type="button"
            aria-expanded={dropdownOpen === "more"}
            onClick={() => setDropdownOpen(dropdownOpen === "more" ? null : "more")}
            className="px-4 py-3 flex w-full gap-1 items-center text-start focus:outline-none"
          >
            <span>{t("COMMON.MORE")}</span>
            {dropdownOpen === "more" ? (
              <ChevronUpIcon className="text-primary-5 font-bold text-[20px]" aria-hidden="true" />
            ) : (
              <ChevronDownIcon className="text-primary-5 font-bold text-[20px]" aria-hidden="true" />
            )}
          </button>
        </li>
        {dropdownOpen === "more" && (
          <ul className="bg-gray-100">
            {moreNavLinks(t).map(({ href, label }) => (
              <li key={href} className="border border-b-primary-5/40 text-primary-5">
                <Link href={href} className="w-full block text-sm ps-4 py-2 pe-6" onClick={close}>{label}</Link>
              </li>
            ))}
          </ul>
        )}

        {/* Auth / Profile in mobile */}
        {!isLoggedIn ? (
          <li className="px-4 py-3 border-b">
            <Link href="/login" onClick={close} className="block w-full">
              <span className="w-full block text-center bg-primary-5 text-white py-2 rounded-xl text-sm font-semibold">
                {t("COMMON.SIGN_IN")}
              </span>
            </Link>
          </li>
        ) : (
          <>
            {/*
              The account items sit behind the avatar now, the way they do on
              desktop, instead of lying flat among the site's pages. They read
              as one person's things rather than five more destinations, and
              the phone menu stops running past the fold.

              Same `dropdownOpen === "profile"` key as the desktop dropdown —
              only one of the two is ever mounted (they are behind
              `mediumscreen:hidden` / `hidden mediumscreen:flex`), so sharing
              it costs nothing and `close()` clears both.
            */}
            <li className="border-b text-primary-5 text-sm">
              <button
                type="button"
                aria-expanded={dropdownOpen === "profile"}
                aria-label={t("COMMON.TOGGLE_PROFILE_MENU") || "Profile menu"}
                onClick={() => setDropdownOpen(dropdownOpen === "profile" ? null : "profile")}
                className="px-4 py-3 flex w-full justify-between items-center text-start focus:outline-none"
              >
                <Image
                  src={profilePicUrl}
                  alt=""
                  width={36}
                  height={36}
                  className="w-9 h-9 object-cover rounded-full border border-primary-5"
                  onError={(e) => {
                    (e.target as HTMLImageElement).src = defaultProfilePic;
                  }}
                  unoptimized
                />
                {dropdownOpen === "profile" ? (
                  <ChevronUpIcon className="text-primary-5 font-bold text-[20px]" aria-hidden="true" />
                ) : (
                  <ChevronDownIcon className="text-primary-5 font-bold text-[20px]" aria-hidden="true" />
                )}
              </button>
            </li>
            {dropdownOpen === "profile" && (
              <ul className="bg-gray-100">
                <li className="border border-b-primary-5/40 text-primary-5">
                  <Link href={isVolunteer ? "/volunteer-profile" : "/entities-profile"} className="w-full block text-sm ps-5 py-2 pe-6" onClick={close}>
                    {/* An organization used to be shown «ملف المتطوع» here —
                        the href already branched, only the label did not. */}
                    {t(isVolunteer ? "COMMON.VOLUNTEER.PROFILE" : "COMMON.ORGANIZATION.PROFILE")}
                  </Link>
                </li>
                {/* <li className="border border-b-primary-5/40 text-primary-5">
                  <Link href="/account-information" className="w-full block text-sm ps-5 py-2 pe-6" onClick={close}>{t("COMMON.ACCOUNT.SETTINGS")}</Link>
                </li> */}
                {/* {isVolunteer && (
                  <li className="border border-b-primary-5/40 text-primary-5">
                    <Link href="/volunteer-qr-code" className="w-full block text-sm ps-5 py-2 pe-6" onClick={close}>{t("COMMON.QRCODE")}</Link>
                  </li>
                )} */}
                {/* Both account types reach My Calendar, matching the desktop
                    dropdown and the `/calendar` route's own guard. */}
                {/* <li className="border border-b-primary-5/40 text-primary-5">
                  <Link href="/calendar" className="w-full block text-sm ps-5 py-2 pe-6" onClick={close}>{t("COMMON.MY.CALENDAR")}</Link>
                </li> */}
                {/* «تقرير» — on the desktop dropdown since it shipped, and
                    missing from the phone menu until now, so the achievement
                    report was simply unreachable on a phone. */}
                {(
                  <li className="border border-b-primary-5/40 text-primary-5">
                    <Link href="/achievement-reports" className="w-full block text-sm ps-5 py-2 pe-6" onClick={close}>{t("COMMON.MY_REPORT")}</Link>
                  </li>
                )}
                {/* BE-129 — the phone menu had no switcher at all. */}
                <MobileAccountActions close={close} handleLogout={handleLogout} handleLogoutAll={handleLogoutAll} />
              </ul>
            )}
          </>
        )}
      </ul>
  );
}
