"use client";

import { useState } from "react";
import { deleteUserAction, updateUserRoleAction } from "@/app/actions/adminActions";
import { MoreVertical, Trash2, Shield, User, Briefcase } from "lucide-react";
import { useRouter } from "next/navigation";

interface UserActionsProps {
    userId: string;
    currentRole: string;
}

export function UserActions({ userId, currentRole }: UserActionsProps) {
    const [isOpen, setIsOpen] = useState(false);
    const [isLoading, setIsLoading] = useState(false);
    const router = useRouter();

    const handleDelete = async () => {
        if (confirm("Are you sure you want to delete this user? This action cannot be undone.")) {
            setIsLoading(true);
            try {
                await deleteUserAction(userId);
                setIsOpen(false);
            } catch (error) {
                console.error("Failed to delete user:", error);
                alert("Failed to delete user.");
            } finally {
                setIsLoading(false);
            }
        }
    };

    const handleRoleChange = async (newRole: "candidate" | "employer" | "admin") => {
        if (confirm(`Change this user's role to ${newRole}?`)) {
            setIsLoading(true);
            try {
                await updateUserRoleAction(userId, newRole);
                setIsOpen(false);
            } catch (error) {
                console.error("Failed to update role:", error);
                alert("Failed to update role.");
            } finally {
                setIsLoading(false);
            }
        }
    };

    return (
        <div className="relative">
            <button
                onClick={() => setIsOpen(!isOpen)}
                className="p-2 hover:bg-slate-100 rounded-lg transition-colors"
                disabled={isLoading}
            >
                <MoreVertical className="h-5 w-5 text-slate-400" />
            </button>

            {isOpen && (
                <>
                    <div
                        className="fixed inset-0 z-10"
                        onClick={() => setIsOpen(false)}
                    />
                    <div className="absolute right-0 top-full mt-2 w-48 bg-white rounded-xl shadow-lg border border-slate-100 z-20 overflow-hidden">
                        <div className="p-1">
                            <div className="px-3 py-2 text-xs font-semibold text-slate-500 uppercase tracking-wider">
                                Change Role
                            </div>
                            {currentRole !== 'candidate' && (
                                <button
                                    onClick={() => handleRoleChange('candidate')}
                                    className="w-full text-left px-3 py-2 text-sm text-slate-700 hover:bg-slate-50 rounded-lg flex items-center gap-2"
                                >
                                    <User className="h-4 w-4 text-emerald-500" />
                                    Make Candidate
                                </button>
                            )}
                            {currentRole !== 'employer' && (
                                <button
                                    onClick={() => handleRoleChange('employer')}
                                    className="w-full text-left px-3 py-2 text-sm text-slate-700 hover:bg-slate-50 rounded-lg flex items-center gap-2"
                                >
                                    <Briefcase className="h-4 w-4 text-purple-500" />
                                    Make Employer
                                </button>
                            )}
                            {currentRole !== 'admin' && (
                                <button
                                    onClick={() => handleRoleChange('admin')}
                                    className="w-full text-left px-3 py-2 text-sm text-slate-700 hover:bg-slate-50 rounded-lg flex items-center gap-2"
                                >
                                    <Shield className="h-4 w-4 text-amber-500" />
                                    Make Admin
                                </button>
                            )}
                            <div className="my-1 border-t border-slate-100" />
                            <button
                                onClick={handleDelete}
                                className="w-full text-left px-3 py-2 text-sm text-red-600 hover:bg-red-50 rounded-lg flex items-center gap-2"
                            >
                                <Trash2 className="h-4 w-4" />
                                Delete User
                            </button>
                        </div>
                    </div>
                </>
            )}
        </div>
    );
}
