import { Link } from "@tanstack/react-router";
import { useEffect, useRef, useState } from "react";
import { useAuth, useLogout } from "../hooks/useAuth";
import { usePublicProfile } from "../hooks/useProfile";
import { LucideIcon } from "../lib/iconData";

export function UserMenu() {
	const [open, setOpen] = useState(false);
	const menuRef = useRef<HTMLDivElement>(null);
	const { logout } = useLogout();
	const { data: auth } = useAuth();
	const userId = auth?.user?.id ? Number(auth.user.id) : null;
	const { data: profile } = usePublicProfile(userId);
	const avatarUrl = profile?.avatarImageUrl;

	useEffect(() => {
		if (!open) return;
		function handleClick(e: MouseEvent) {
			if (menuRef.current && !menuRef.current.contains(e.target as Node)) {
				setOpen(false);
			}
		}
		document.addEventListener("mousedown", handleClick);
		return () => document.removeEventListener("mousedown", handleClick);
	}, [open]);

	return (
		<div ref={menuRef} className="relative">
			<button
				type="button"
				onClick={() => setOpen((prev) => !prev)}
				className="flex items-center justify-center w-8 h-8 rounded-full text-gray-500 hover:text-gray-700 hover:bg-gray-100 transition-colors overflow-hidden"
			>
				{avatarUrl ? (
					<img
						src={avatarUrl}
						alt="Profile"
						className="w-8 h-8 rounded-full object-cover"
					/>
				) : (
					<LucideIcon name="circle-user" size={22} />
				)}
			</button>
			{open && (
				<div className="absolute right-0 mt-1 w-40 bg-white rounded-lg shadow-lg border border-gray-200 py-1 z-50">
					<Link
						to="/profile"
						onClick={() => setOpen(false)}
						className="flex items-center gap-2 px-3 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors"
					>
						<LucideIcon
							name="circle-user"
							size={16}
							className="text-gray-400"
						/>
						Profile
					</Link>
					<Link
						to="/settings"
						onClick={() => setOpen(false)}
						className="flex items-center gap-2 px-3 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors"
					>
						<LucideIcon name="settings" size={16} className="text-gray-400" />
						Settings
					</Link>
					<div className="border-t border-gray-100 my-1" />
					<button
						type="button"
						onClick={() => {
							setOpen(false);
							logout();
						}}
						className="flex items-center gap-2 w-full px-3 py-2 text-sm text-gray-700 hover:bg-gray-50 transition-colors"
					>
						<LucideIcon name="log-out" size={16} className="text-gray-400" />
						Sign out
					</button>
				</div>
			)}
		</div>
	);
}
