"use client"

import { useTransition } from "react"
import { useRouter } from "next/navigation"
import { deleteEntryAction } from "@/app/admin/actions"

interface DeleteEntryButtonProps {
  entryId: string
  entryTitle: string
}

export function DeleteEntryButton({ entryId, entryTitle }: DeleteEntryButtonProps) {
  const router = useRouter()
  const [isPending, startTransition] = useTransition()

  const handleDelete = async () => {
    const confirmed = window.confirm(
      `Ești sigur că vrei să ștergi contribuția "${entryTitle}"? Această acțiune este ireversibilă.`
    )

    if (!confirmed) return

    startTransition(async () => {
      try {
        await deleteEntryAction(entryId)
        router.push("/admin")
        router.refresh()
      } catch (error) {
        console.error("Failed to delete entry:", error)
        alert("A apărut o eroare la ștergerea contribuției.")
      }
    })
  }

  return (
    <button
      type="button"
      onClick={handleDelete}
      disabled={isPending}
      style={{
        padding: "0.75rem 1.5rem",
        borderRadius: "var(--radius-md)",
        color: "var(--danger)",
        background: "transparent",
        border: "1px solid var(--danger)",
        fontWeight: 500,
        cursor: isPending ? "not-allowed" : "pointer",
        opacity: isPending ? 0.7 : 1,
        fontSize: "0.95rem"
      }}
    >
      {isPending ? "Se șterge..." : "Șterge"}
    </button>
  )
}
