import React from 'react';
import { X } from 'lucide-react';
import { Button } from '@/components/ui/button';

interface CustomDialogProps {
  isOpen: boolean;
  onClose: () => void;
  title: string;
  description?: string;
  children: React.ReactNode;
  allowClose?: boolean;
  isPending?: boolean;
}

const CustomDialog: React.FC<CustomDialogProps> = ({
  isOpen,
  onClose,
  title,
  description,
  children,
  allowClose = true,
  isPending = false,
}) => {
  if (!isOpen) return null;

  const handleOverlayClick = (e: React.MouseEvent) => {
    // Only allow closing if explicitly enabled and not pending
    if (allowClose && !isPending && e.target === e.currentTarget) {
      onClose();
    }
  };

  return (
    <div className="fixed inset-0 z-[999] flex items-center justify-center p-4 overflow-hidden">
      {/* Overlay */}
      <div 
        className="fixed inset-0 bg-black/80 backdrop-blur-sm"
        onClick={handleOverlayClick}
      />
      
      {/* Modal Content */}
      <div className="relative z-[1000] w-full max-w-3xl rounded-lg border bg-background p-6 shadow-lg flex flex-col max-h-[90vh] overflow-hidden">
        {/* Header */}
        <div className="mb-4 flex-shrink-0">
          <h2 className="text-lg font-semibold">{title}</h2>
          {description && <p className="text-sm text-muted-foreground">{description}</p>}
        </div>
        
        {/* Body - Add auto overflow to allow content to scroll */}
        <div className="flex-1 overflow-auto">
          {children}
        </div>
        
        {/* Close button - only if allowed */}
        {allowClose && !isPending && (
          <button
            onClick={onClose}
            className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none z-[1001]"
          >
            <X className="h-4 w-4" />
            <span className="sr-only">Close</span>
          </button>
        )}
      </div>
    </div>
  );
};

export default CustomDialog; 