"use client"

import { useTransition } from "react"

import { deleteUser } from "@/lib/admin/actions"

type DeleteUserButtonProps = {
  userId: string
}

export default function DeleteUserButton({ userId }: DeleteUserButtonProps) {
  const [isPending, startTransition] = useTransition()

  return (
    <button
      type="button"
      disabled={isPending}
      onClick={() => {
        if (!window.confirm("Delete this user? This cannot be undone.")) {
          return
        }

        startTransition(async () => {
          await deleteUser(userId)
        })
      }}
      className="cursor-pointer bg-transparent text-[13px] text-[#ef4444] hover:underline disabled:cursor-not-allowed disabled:opacity-60"
    >
      {isPending ? "Deleting..." : "Delete"}
    </button>
  )
}
