'use client';

import { AlertTriangle, HelpCircle } from 'lucide-react';

import { FormAlert } from '@/components/forms/form-field';
import { Button } from '@/components/ui/button';
import { Modal } from '@/components/ui/overlay';
import { cn } from '@/lib/utils';

export function ConfirmDialog({
  open,
  title,
  message,
  confirmLabel = 'Confirm',
  cancelLabel = 'Cancel',
  error,
  loading = false,
  destructive = true,
  onConfirm,
  onClose,
}: {
  open: boolean;
  title: string;
  message: string;
  confirmLabel?: string;
  cancelLabel?: string;
  error?: string | null;
  loading?: boolean;
  destructive?: boolean;
  onConfirm: () => void;
  onClose: () => void;
}) {
  const Icon = destructive ? AlertTriangle : HelpCircle;
  return (
    <Modal open={open} title={title} onClose={onClose}>
      <div className="flex items-start gap-4">
        <span className={cn('grid h-10 w-10 shrink-0 place-items-center rounded-full', destructive ? 'bg-danger-soft text-danger' : 'bg-primary-soft text-primary')} aria-hidden="true">
          <Icon className="h-5 w-5" />
        </span>
        <p className="pt-2 text-body text-muted-foreground">{message}</p>
      </div>
      {error === undefined || error === null ? null : <FormAlert className="mt-4">{error}</FormAlert>}
      <div className="mt-6 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end">
        <Button type="button" variant="secondary" onClick={onClose} disabled={loading}>
          {cancelLabel}
        </Button>
        <Button type="button" variant={destructive ? 'destructive' : 'primary'} loading={loading} onClick={onConfirm}>
          {confirmLabel}
        </Button>
      </div>
    </Modal>
  );
}
