'use client'

import * as React from 'react'
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger
} from '@admin/components/ui/alert-dialog'
import { Button } from '@admin/components/ui/button'
import { Trash2 } from 'lucide-react'

interface DeleteDialogProps {
  count: number
  entityName: string
  onConfirm: () => void
  isPending?: boolean
}

export function DeleteDialog({
  count,
  entityName,
  onConfirm,
  isPending = false
}: DeleteDialogProps) {
  const [open, setOpen] = React.useState(false)
  const label = count === 1 ? entityName : `${entityName}s`
  const targetLabel = count === 1 ? `this ${label}` : `these ${label}`

  return (
    <AlertDialog open={open} onOpenChange={setOpen}>
      <AlertDialogTrigger render={<Button variant="destructive" size="default" />}>
        <Trash2 className="-ml-0.5 size-3.5" strokeWidth={2} />
        Delete {count} {label}
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>
            Delete {count} {label}?
          </AlertDialogTitle>
          <AlertDialogDescription>
            This permanently deletes {targetLabel} and cannot be undone.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel disabled={isPending}>Cancel</AlertDialogCancel>
          <AlertDialogAction
            onClick={(e) => {
              e.preventDefault()
              onConfirm()
            }}
            disabled={isPending}
            aria-busy={isPending}
          >
            {isPending ? 'Deleting…' : `Delete ${count} ${label}`}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}
