import { useEffect } from 'react';
import { Icon } from '../../Atoms/Icon/Icon';
import { Button } from '../../Atoms/Button/Button';

export const Drawer = ({
  isOpen = false,
  onClose,
  title,
  children,
  position = 'right',
  size = 'medium',
  showCloseButton = true,
  className = '',
  ...props
}) => {
  useEffect(() => {
    if (isOpen) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = 'unset';
    }
    return () => {
      document.body.style.overflow = 'unset';
    };
  }, [isOpen]);

  if (!isOpen) return null;

  const positionClasses = {
    left: 'left-0',
    right: 'right-0',
    top: 'top-0 left-0 right-0',
    bottom: 'bottom-0 left-0 right-0',
  };

  const sizeClasses = {
    small: position === 'left' || position === 'right' ? 'w-80' : 'h-64',
    medium: position === 'left' || position === 'right' ? 'w-96' : 'h-96',
    large: position === 'left' || position === 'right' ? 'w-[32rem]' : 'h-[32rem]',
    full: position === 'left' || position === 'right' ? 'w-screen' : 'h-screen',
  };

  return (
    <>
      <div
        className="fixed inset-0 z-40 bg-gray-500 bg-opacity-75 transition-opacity"
        onClick={onClose}
      />
      <div
        className={`fixed z-50 bg-white shadow-xl ${positionClasses[position]} ${sizeClasses[size]} ${className}`}
        {...props}
      >
        {title && (
          <div className="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
            <h3 className="text-lg font-semibold text-gray-900">{title}</h3>
            {showCloseButton && (
              <button
                onClick={onClose}
                className="text-gray-400 hover:text-gray-500"
              >
                <Icon name="XMarkIcon" className="h-6 w-6" />
              </button>
            )}
          </div>
        )}
        <div className="p-6 overflow-y-auto" style={{ maxHeight: 'calc(100vh - 80px)' }}>
          {children}
        </div>
      </div>
    </>
  );
};

export default Drawer;

