"use client";

import {
  ArrowUpRightIcon,
  ChevronRightIcon,
  LogOutIcon,
  MegaphoneIcon,
  MessageSquareTextIcon,
  UserCircleIcon,
} from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useTranslation } from "react-i18next";
import formbricks from "@formbricks/js";
import type { TUser } from "@formbricks/types/user";
import { cn } from "@/lib/cn";
import { useSignOut } from "@/modules/auth/hooks/use-sign-out";
import { ProfileAvatar } from "@/modules/ui/components/avatars";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/modules/ui/components/dropdown-menu";

interface UserDropdownProps {
  user: TUser;
  organizationId: string;
  isCollapsed?: boolean;
  isTextVisible?: boolean;
  className?: string;
  // Whether the Formbricks-in-Formbricks in-app survey widget is configured (workspace id set). The
  // "What's New" and "Share feedback" items only trigger Formbricks surveys, so they are hidden when
  // the widget is not mounted — otherwise they would render but do nothing.
  isFormbricksSurveysConfigured?: boolean;
}

// The avatar/account trigger + menu (Account, Documentation, Share feedback, Log out) shown at the
// bottom of the sidebar. Extracted so the standalone org/account settings shell renders the same
// user menu the workspace navigation does — otherwise those routes have no way to log out, which
// strands billing-role users who get redirected straight into settings.
export const UserDropdown = ({
  user,
  organizationId,
  isCollapsed = false,
  isTextVisible = false,
  className,
  isFormbricksSurveysConfigured = false,
}: Readonly<UserDropdownProps>) => {
  const { t } = useTranslation();
  const router = useRouter();
  const { signOut: signOutWithAudit } = useSignOut({ id: user.id, email: user.email });

  const dropdownNavigation = [
    {
      label: t("common.account"),
      href: "/account/settings/profile",
      icon: UserCircleIcon,
    },
    {
      label: t("common.documentation"),
      href: "https://formbricks.com/docs",
      target: "_blank",
      icon: ArrowUpRightIcon,
    },
  ];

  const triggerClasses = cn(
    "w-full border-t px-3 py-3 text-left transition-colors duration-200 hover:bg-slate-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-slate-500 focus-visible:ring-inset",
    isCollapsed ? "flex items-center justify-center" : "",
    className
  );
  const iconClasses =
    "flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-slate-100 text-slate-600";

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild id="userDropdownTrigger" className={triggerClasses}>
        <button
          type="button"
          aria-label={isCollapsed ? t("common.account_settings") : undefined}
          className={cn("flex w-full items-center gap-3", isCollapsed && "justify-center")}>
          <span className={iconClasses}>
            <ProfileAvatar userId={user.id} />
          </span>
          {!isCollapsed && !isTextVisible && (
            <>
              <div className="grow overflow-hidden">
                <p
                  title={user?.email}
                  className="ph-no-capture -mb-0.5 truncate text-sm font-bold text-slate-700">
                  {user?.name ? <span>{user?.name}</span> : <span>{user?.email}</span>}
                </p>
                <p className="text-sm text-slate-500">{t("common.account")}</p>
              </div>
              <ChevronRightIcon className="size-4 shrink-0 text-slate-600" strokeWidth={1.5} />
            </>
          )}
        </button>
      </DropdownMenuTrigger>

      <DropdownMenuContent
        id="userDropdownInnerContentWrapper"
        side="right"
        sideOffset={10}
        alignOffset={5}
        align="end">
        {dropdownNavigation.map((link) => (
          <Link
            href={link.href}
            target={link.target}
            className="flex w-full items-center"
            key={link.label}
            rel={link.target === "_blank" ? "noopener noreferrer" : undefined}>
            <DropdownMenuItem>
              <link.icon className="mr-2 size-4" strokeWidth={1.5} />
              {link.label}
            </DropdownMenuItem>
          </Link>
        ))}
        {isFormbricksSurveysConfigured && (
          <>
            <DropdownMenuItem
              onClick={() => formbricks.track("whats_new_clicked").catch(() => undefined)}
              icon={<MegaphoneIcon className="mr-2 size-4" strokeWidth={1.5} />}>
              {t("common.whats_new")}
            </DropdownMenuItem>
            <DropdownMenuItem
              onClick={() => formbricks.track("share_feedback_clicked").catch(() => undefined)}
              icon={<MessageSquareTextIcon className="mr-2 size-4" strokeWidth={1.5} />}>
              {t("common.share_feedback")}
            </DropdownMenuItem>
          </>
        )}
        <DropdownMenuItem
          onClick={async () => {
            // Relative on purpose: the admin app and the public survey domain are different hosts when
            // PUBLIC_URL is set, and Proxy 404s every non-survey route on the public one. An absolute
            // URL built from publicDomain sent people to a 404 instead of the login page.
            const loginUrl = "/auth/login";
            const route = await signOutWithAudit({
              reason: "user_initiated",
              redirectUrl: loginUrl,
              organizationId,
              redirect: false,
              callbackUrl: loginUrl,
              clearWorkspaceId: true,
            });
            router.push(route?.url || loginUrl);
          }}
          icon={<LogOutIcon className="mr-2 size-4" strokeWidth={1.5} />}>
          {t("common.logout")}
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
};
