'use client'

import { useEffect, useRef } from 'react'
{{#IF_DESIGN_FRAMER_MOTION}}
import { AnimatePresence, motion } from 'framer-motion'
{{/IF_DESIGN_FRAMER_MOTION}}

/**
 * Modal — Dialog overlay
 * Generated by Traqr Init ({{DESIGN_FLAVOR}} flavor). Edit freely.
 *
 * Usage:
 *   <Modal open={isOpen} onClose={() => setIsOpen(false)} title="Confirm">
 *     <p>Are you sure?</p>
 *   </Modal>
 */

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

export function Modal({ open, onClose, title, children, className = '' }: ModalProps) {
  const overlayRef = useRef<HTMLDivElement>(null)

  // Close on Escape
  useEffect(() => {
    if (!open) return
    function handleKey(e: KeyboardEvent) {
      if (e.key === 'Escape') onClose()
    }
    document.addEventListener('keydown', handleKey)
    return () => document.removeEventListener('keydown', handleKey)
  }, [open, onClose])

  // Close on overlay click
  function handleOverlayClick(e: React.MouseEvent) {
    if (e.target === overlayRef.current) onClose()
  }

{{#IF_DESIGN_FRAMER_MOTION}}
  return (
    <AnimatePresence>
      {open && (
        <motion.div
          ref={overlayRef}
          className="fixed inset-0 z-[200] flex items-center justify-center bg-black/50 p-4"
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
          onClick={handleOverlayClick}
        >
          <motion.div
            className={`w-full max-w-md bg-design-card border border-design-border {{DESIGN_RADIUS_LG}} design-shadow-lg ${className}`}
            initial={{ opacity: 0, scale: 0.95, y: 10 }}
            animate={{ opacity: 1, scale: 1, y: 0 }}
            exit={{ opacity: 0, scale: 0.95, y: 10 }}
            transition={{ type: 'spring', stiffness: {{DESIGN_SPRING_STIFFNESS}}, damping: {{DESIGN_SPRING_DAMPING}} }}
          >
            {title && (
              <div className="flex items-center justify-between border-b border-design-border px-5 py-3">
                <h2 className="text-lg font-semibold text-design-fg">{title}</h2>
                <button onClick={onClose} className="text-design-muted hover:text-design-fg design-transition">
                  <svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
                  </svg>
                </button>
              </div>
            )}
            <div className="p-5">{children}</div>
          </motion.div>
        </motion.div>
      )}
    </AnimatePresence>
  )
{{/IF_DESIGN_FRAMER_MOTION}}

  if (!open) return null

  return (
    <div
      ref={overlayRef}
      className="fixed inset-0 z-[200] flex items-center justify-center bg-black/50 p-4"
      onClick={handleOverlayClick}
    >
      <div className={`w-full max-w-md bg-design-card border border-design-border {{DESIGN_RADIUS_LG}} design-shadow-lg design-animate-in ${className}`}>
        {title && (
          <div className="flex items-center justify-between border-b border-design-border px-5 py-3">
            <h2 className="text-lg font-semibold text-design-fg">{title}</h2>
            <button onClick={onClose} className="text-design-muted hover:text-design-fg design-transition">
              <svg className="h-5 w-5" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
              </svg>
            </button>
          </div>
        )}
        <div className="p-5">{children}</div>
      </div>
    </div>
  )
}
