'use client'

import React, { useEffect } from 'react'
import { IoClose } from 'react-icons/io5'

interface ModalProps {
  isOpen: boolean
  onClose: () => void
  title: string
  children: React.ReactNode
}

const Modal: React.FC<ModalProps> = ({ isOpen, onClose, title, children }) => {
  useEffect(() => {
    const handleEscape = (e: KeyboardEvent) => {
      if (e.key === 'Escape') {
        onClose()
      }
    }

    if (isOpen) {
      document.addEventListener('keydown', handleEscape)
      const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth
      document.body.style.paddingRight = `${scrollbarWidth}px`
      document.body.style.overflow = 'hidden'
    }

    return () => {
      document.removeEventListener('keydown', handleEscape)
      document.body.style.paddingRight = '0px'
      document.body.style.overflow = 'unset'
    }
  }, [isOpen, onClose])

  if (!isOpen) return null

  return (
    <div className="fixed inset-0 z-[9999] overflow-y-auto">
      <div
        className={`fixed inset-0 bg-black transition-all duration-500 ease-in-out ${isOpen ? 'opacity-50 backdrop-blur-sm' : 'opacity-0 backdrop-blur-none'} `}
        onClick={onClose}
      />

      <div className="z-[10000] flex min-h-full items-center justify-center">
        <div
          className={`relative min-h-screen w-full transform overflow-hidden bg-white text-left shadow-xl transition-all duration-500 ease-in-out sm:m-4 sm:min-h-0 sm:max-w-3xl sm:rounded-2xl ${
            isOpen ? 'translate-y-0 scale-100 opacity-100' : 'translate-y-8 scale-95 opacity-0'
          } `}
        >
          <div className="flex items-center justify-between border-b border-gray-100 p-4 sm:p-6">
            <h2 className="text-xl font-bold sm:text-2xl">{title}</h2>
            <button
              onClick={onClose}
              className="rounded-xl bg-gray-500 p-2 text-white transition-all duration-300 ease-in-out hover:scale-105 hover:bg-gray-400 hover:text-black"
            >
              <IoClose size={24} />
            </button>
          </div>

          {children}
        </div>
      </div>
    </div>
  )
}

export default Modal
