"use client";

import { useState } from "react";
import { Trash2, Loader2 } from "lucide-react";
import { useRouter } from "next/navigation";
import toast from "react-hot-toast";

export default function AdminProductActions({ productId }: { productId: string }) {
  const [loading, setLoading] = useState(false);
  const router = useRouter();

  const handleDelete = async () => {
    if (!confirm("Delete this product? This action cannot be undone.")) return;
    setLoading(true);
    const res = await fetch(`/api/admin/products/${productId}`, { method: "DELETE" });
    setLoading(false);
    if (res.ok) { toast.success("Product deleted"); router.refresh(); }
    else toast.error("Failed to delete product");
  };

  return (
    <button onClick={handleDelete} disabled={loading}
      className="p-2 rounded-lg hover:bg-red-50 text-red-500 transition-colors disabled:opacity-50">
      {loading ? <Loader2 className="w-4 h-4 animate-spin" /> : <Trash2 className="w-4 h-4" />}
    </button>
  );
}
