"use client";

import Link from "next/link";
import { Heart, LayoutDashboard, LogOut, Package, User } from "lucide-react";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { logoutAction } from "@/app/actions/auth";
import type { Locale } from "@/lib/i18n/config";

export function AccountMenu({
  locale,
  user,
  labels,
}: {
  locale: Locale;
  user: { name: string; email: string; role: string } | null;
  labels: {
    account: string;
    login: string;
    orders: string;
    wishlist: string;
    admin: string;
    logout: string;
    greeting: string;
  };
}) {
  if (!user) {
    return (
      <Link
        href={`/${locale}/login`}
        className="p-2 text-charcoal/75 transition-colors hover:text-charcoal"
        aria-label={labels.login}
      >
        <User className="h-[18px] w-[18px]" strokeWidth={1.4} />
      </Link>
    );
  }

  return (
    <DropdownMenu>
      <DropdownMenuTrigger
        className="p-2 text-charcoal/75 transition-colors hover:text-charcoal"
        aria-label={labels.account}
      >
        <User className="h-[18px] w-[18px]" strokeWidth={1.4} />
      </DropdownMenuTrigger>
      <DropdownMenuContent align="end" className="w-60">
        <DropdownMenuLabel>
          {labels.greeting}, {user.name.split(" ")[0]}
        </DropdownMenuLabel>
        <DropdownMenuItem asChild>
          <Link href={`/${locale}/account`}>
            <User className="h-3.5 w-3.5" /> {labels.account}
          </Link>
        </DropdownMenuItem>
        <DropdownMenuItem asChild>
          <Link href={`/${locale}/account/orders`}>
            <Package className="h-3.5 w-3.5" /> {labels.orders}
          </Link>
        </DropdownMenuItem>
        <DropdownMenuItem asChild>
          <Link href={`/${locale}/account/wishlist`}>
            <Heart className="h-3.5 w-3.5" /> {labels.wishlist}
          </Link>
        </DropdownMenuItem>
        {(user.role === "ADMIN" || user.role === "SUPERADMIN") && (
          <DropdownMenuItem asChild>
            <Link href="/admin">
              <LayoutDashboard className="h-3.5 w-3.5" /> {labels.admin}
            </Link>
          </DropdownMenuItem>
        )}
        <DropdownMenuSeparator />
        <DropdownMenuItem asChild>
          <form action={logoutAction} className="w-full">
            <input type="hidden" name="locale" value={locale} />
            <button type="submit" className="flex w-full items-center gap-2 text-left">
              <LogOut className="h-3.5 w-3.5" /> {labels.logout}
            </button>
          </form>
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}
