"use client";

import { useTransition } from "react";
import { LuTrash2 } from "react-icons/lu";

// --- INICIO: Mock de la acción para previsualización ---
// En tu aplicación real, deberías eliminar esto y usar tu import real.
const eliminarTodosLosDocumentos = async (): Promise<{
  success: boolean;
  count?: number;
  error?: string;
}> => {
  await new Promise((res) => setTimeout(res, 1500));
  // Cambia esto para simular un error
  const success = true;
  if (success) {
    const count = Math.floor(Math.random() * 20) + 1;
    return { success: true, count };
  } else {
    return { success: false, error: "Error simulado de base de datos" };
  }
};
// --- FIN: Mock de la acción ---

export default function BotonEliminarTodoDev() {
  const [isPending, startTransition] = useTransition();

  const handleBorrarTodo = () => {
    // PREGUNTA DE SEGURIDAD: Es crucial confirmar una acción tan destructiva.
    if (
      window.confirm(
        "🔴 ¿ESTÁS COMPLETAMENTE SEGURO?\n\nVas a borrar TODOS los documentos de la base de datos. Esta acción no se puede deshacer."
      )
    ) {
      startTransition(async () => {
        const resultado = await eliminarTodosLosDocumentos();
        if (resultado.success) {
          alert(`¡Éxito! Se eliminaron ${resultado.count} documentos.`);
          // Recargamos la página para ver la tabla vacía.
          window.location.reload(); // Esto no funcionará en el sandbox, pero es correcto para tu app
        } else {
          alert(`Error: ${resultado.error}`);
        }
      });
    }
  };

  return (
    <button
      onClick={handleBorrarTodo}
      disabled={isPending}
      // Clases actualizadas:
      // text-red-600 -> text-destructive
      // bg-red-50 -> bg-destructive-background
      // border-red-200 -> border-destructive-border
      // hover:bg-red-100 -> hover:bg-destructive-background hover:brightness-95 (para simular el oscurecimiento)
      className="cursor-pointer flex items-center gap-2 px-3 py-2 text-sm font-medium text-destructive bg-destructive-background border border-destructive-border rounded-lg hover:bg-destructive-background hover:brightness-95 disabled:opacity-50 disabled:cursor-not-allowed"
    >
      <LuTrash2 className="h-4 w-4" />
      {isPending ? "Eliminando..." : "Eliminar Todo (DEV)"}
    </button>
  );
}
