"use client";

import { useTransition } from 'react';
import { deleteBlogPost } from '@/app/actions/actualites';
import { Trash2, Loader2 } from 'lucide-react';
import { toast } from 'react-hot-toast';

export default function DeleteActualitesButton({ id }: { id: string }) {
  const [isPending, startTransition] = useTransition();

  const handleDelete = () => {
    if (confirm("Supprimer cet article ?")) {
      startTransition(async () => {
        const result = await deleteBlogPost(id);
        if (result.success) {
          toast.success("Article supprimé");
        } else {
          toast.error(result.error || "Erreur lors de la suppression");
        }
      });
    }
  };

  return (
    <button onClick={handleDelete} disabled={isPending} className="p-2 text-slate-500 hover:text-red-400">
      {isPending ? <Loader2 className="w-5 h-5 animate-spin" /> : <Trash2 className="w-5 h-5" />}
    </button>
  );
}
