'use client'
import {Button} from '@/components/ui/button'
import {Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger} from '@/components/ui/dialog'
import {useRouter} from 'next/navigation'
import {useState} from 'react'

interface RealnameAuthDialogProps {
  hasAuthenticated: boolean
  triggerClassName?: string
  open?: boolean
  onOpenChange?: (open: boolean) => void
  onSuccess?: () => void
}

export function RealnameAuthDialog({
  hasAuthenticated,
  triggerClassName,
  open,
  onOpenChange,
  onSuccess,
}: RealnameAuthDialogProps) {
  const [internalOpen, setInternalOpen] = useState(false)
  const router = useRouter()

  const actualOpen = open !== undefined ? open : internalOpen
  const actualOnOpenChange = onOpenChange || setInternalOpen

  if (hasAuthenticated) {
    return null
  }

  return (
    <Dialog open={actualOpen} onOpenChange={actualOnOpenChange}>
      <DialogTrigger asChild>
        <Button theme="outline" className={triggerClassName || 'w-24'}>
          去认证
        </Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>实名认证</DialogTitle>
        </DialogHeader>
        <div className="py-4 space-y-4">
          <p>为了保障您的账户安全和正常使用服务，请您尽快完成实名认证</p>
          <div className="flex justify-end gap-2">
            <Button
              theme="outline"
              onClick={() => actualOnOpenChange(false)}
            >
              稍后再说
            </Button>
            <Button
              onClick={() => {
                router.push('/admin/identify')
                onSuccess?.()
              }}
            >
              立即认证
            </Button>
          </div>
        </div>
      </DialogContent>
    </Dialog>
  )
}
