"use client";

import { useState } from "react";
import { deleteJobAction, toggleJobFeaturedAction } from "@/app/actions/adminActions";
import { MoreVertical, Trash2, CheckCircle2 } from "lucide-react";
import { useRouter } from "next/navigation";

interface JobActionsProps {
    jobId: string;
    isFeatured: boolean;
}

export function JobActions({ jobId, isFeatured }: JobActionsProps) {
    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 job? This action cannot be undone.")) {
            setIsLoading(true);
            try {
                await deleteJobAction(jobId);
                setIsOpen(false);
            } catch (error) {
                console.error("Failed to delete job:", error);
                alert("Failed to delete job.");
            } finally {
                setIsLoading(false);
            }
        }
    };

    const handleToggleFeatured = async () => {
        setIsLoading(true);
        try {
            await toggleJobFeaturedAction(jobId);
            setIsOpen(false);
        } catch (error) {
            console.error("Failed to toggle featured status:", error);
            alert("Failed to update job.");
        } 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">
                            <button
                                onClick={handleToggleFeatured}
                                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"
                            >
                                <CheckCircle2 className={`h-4 w-4 ${isFeatured ? 'text-amber-500' : 'text-slate-400'}`} />
                                {isFeatured ? 'Unfeature Job' : 'Feature Job'}
                            </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 Job
                            </button>
                        </div>
                    </div>
                </>
            )}
        </div>
    );
}
