'use client'

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from '@/components/ui/alert-dialog'
import { useNimConversation } from '@/context/NimChat/useNimChat'
import { useRouter } from 'next/navigation'
import { useState } from 'react'

const ChatConversationsDeleteDialog = ({
  open,
  onOpenChange,
}: {
  open: boolean
  onOpenChange: (open: boolean) => void
}) => {
  const { clearAllConversations } = useNimConversation()
  const [loading, setLoading] = useState(false)
  const router = useRouter()

  const handleClick = async () => {
    setLoading(true)
    await clearAllConversations()
    onOpenChange(false)
    setLoading(false)
    router.replace('/')
  }

  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Clear Chat List</AlertDialogTitle>
        </AlertDialogHeader>
        <AlertDialogDescription>
          This will clear your chat list. Your individual messages will remain intact.
        </AlertDialogDescription>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction variant="destructive" loading={loading} onClick={handleClick}>
            Clear
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

export default ChatConversationsDeleteDialog
