import { ReactNode } from 'react'
import { useTranslation } from 'react-i18next'
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/shared/ui/dialog'
import { Button } from '@/shared/ui/button'

interface ConfirmDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  title: string
  description?: string | ReactNode
  confirmText?: string
  cancelText?: string
  variant?: 'default' | 'destructive'
  onConfirm: () => void | Promise<void>
  contentTestId?: string
  confirmButtonTestId?: string
}

export function ConfirmDialog({
  open,
  onOpenChange,
  title,
  description,
  confirmText,
  cancelText,
  variant = 'default',
  onConfirm,
  contentTestId,
  confirmButtonTestId,
}: ConfirmDialogProps) {
  const { t } = useTranslation()
  const resolvedConfirmText = confirmText ?? t('dialog.confirm')
  const resolvedCancelText = cancelText ?? t('dialog.cancel')
  const handleConfirm = async () => {
    await onConfirm()
    onOpenChange(false)
  }

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent data-testid={contentTestId}>
        <DialogHeader className="min-w-0 text-center sm:text-center">
          <DialogTitle className="text-center">{title}</DialogTitle>
          {description && <DialogDescription className="text-center break-all">{description}</DialogDescription>}
        </DialogHeader>
        <DialogFooter className="sm:justify-center sm:space-x-3">
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            {resolvedCancelText}
          </Button>
          <Button data-testid={confirmButtonTestId} variant={variant} onClick={handleConfirm}>
            {resolvedConfirmText}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}
