"use client";

import { motion, AnimatePresence } from 'framer-motion';
import { useEffect } from 'react';
import ContactForm from './ContactForm';
import { useModal } from '@/context/ModalContext';
import { useLocale, useTranslations } from 'next-intl';

export default function ApplicationModal() {
  const { isModalOpen, closeModal } = useModal();
  const locale = useLocale();
  const t = useTranslations('apply');

  // Modal açıldığında sayfanın kaydırılmasını engelle
  useEffect(() => {
    if (isModalOpen) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = 'unset';
    }
    
    return () => {
      document.body.style.overflow = 'unset';
    };
  }, [isModalOpen]);

  // ESC tuşuna basıldığında modalı kapat
  useEffect(() => {
    const handleEsc = (e: KeyboardEvent) => {
      if (e.key === 'Escape') {
        closeModal();
      }
    };
    window.addEventListener('keydown', handleEsc);
    return () => {
      window.removeEventListener('keydown', handleEsc);
    };
  }, [closeModal]);

  return (
    <AnimatePresence>
      {isModalOpen && (
        <>
          {/* Arka plan overlay */}
          <motion.div
            className="fixed inset-0 bg-black/80 backdrop-blur-sm z-50"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            onClick={closeModal}
          />
          
          {/* Modal içeriği */}
          <motion.div
            className="fixed inset-0 z-50 flex items-center justify-center p-4 sm:p-6 md:p-8"
            onClick={(e) => e.stopPropagation()}
          >
            <motion.div 
              className="bg-gradient-to-br from-slate-800/95 to-slate-900/95 rounded-2xl border border-white/10 shadow-2xl w-full max-w-5xl max-h-[90vh] overflow-hidden flex flex-col"
              initial={{ opacity: 0, y: 50, scale: 0.9 }}
              animate={{ opacity: 1, y: 0, scale: 1 }}
              exit={{ opacity: 0, y: 50, scale: 0.9 }}
              transition={{ type: "spring", damping: 25, stiffness: 300 }}
            >
              {/* Kapatma butonu */}
              <button 
                onClick={closeModal}
                className="absolute top-5 right-5 p-3 rounded-full bg-white/10 hover:bg-white/20 transition-colors z-10 shadow-lg hover:shadow-blue-900/20"
                aria-label={locale === 'tr' ? 'Kapat' : 'Close'}
              >
                <svg className="w-6 h-6 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M6 18L18 6M6 6l12 12" />
                </svg>
              </button>
              
              {/* ContactForm bileşeni - Kaydırılabilir içerik alanı */}
              <div className="overflow-y-auto custom-scrollbar flex-1 p-6 sm:p-8 md:p-10">
                <div className="mb-8">
                  <h2 className="text-3xl font-bold text-white mb-3 flex items-center">
                    <span className="bg-clip-text text-transparent bg-gradient-to-r from-blue-400 to-indigo-300">
                      {t('title')}
                    </span>
                    <motion.div 
                      className="ml-2 w-3 h-3 rounded-full bg-blue-500"
                      animate={{ 
                        scale: [1, 1.2, 1],
                        opacity: [0.7, 1, 0.7]
                      }}
                      transition={{ duration: 2, repeat: Infinity, type: "tween" }}
                    />
                  </h2>
                  <p className="text-blue-100/80 max-w-2xl">
                    {locale === 'tr' ? 
                      'GM2 ile girişiminizi bir sonraki seviyeye taşımak için başvuru formunu doldurun. MÜSİAD\'ın deneyimli ağı, mentorları ve kaynakları ile girişiminizi büyütün.' :
                      'Fill out the application form to take your venture to the next level with GM2. Grow your startup with MÜSIAD\'s experienced network, mentors and resources.'
                    }
                  </p>
                  <div className="mt-4">
                    <h3 className="text-lg font-medium text-white mb-2">
                      {locale === 'tr' ? 'Başvuru Adımları:' : 'Application Steps:'}
                    </h3>
                    <ul className="list-disc list-inside space-y-1 text-blue-100/70">
                      <li>
                        <span className="font-medium text-blue-300">{t('step1')}</span>
                        {locale === 'tr' ? ' - İletişim bilgilerinizi paylaşın' : ' - Share your contact information'}
                      </li>
                      <li>
                        <span className="font-medium text-blue-300">{t('step2')}</span>
                        {locale === 'tr' ? ' - Girişiminizin detaylarını anlatın' : ' - Tell us about your venture details'}
                      </li>
                      <li>
                        <span className="font-medium text-blue-300">{t('step3')}</span>
                        {locale === 'tr' ? ' - Ek bilgileri tamamlayın' : ' - Complete additional information'}
                      </li>
                    </ul>
                  </div>
                  <div className="h-px w-full bg-gradient-to-r from-blue-500/20 to-transparent my-6" />
                </div>
                <ContactForm isModal={true} onSubmitSuccess={closeModal} />
              </div>
            </motion.div>
          </motion.div>
          
          {/* Özel kaydırma çubuğu stilleri için CSS */}
          <style jsx global>{`
            .custom-scrollbar::-webkit-scrollbar {
              width: 10px;
            }
            
            .custom-scrollbar::-webkit-scrollbar-track {
              background: rgba(255, 255, 255, 0.05);
              border-radius: 10px;
            }
            
            .custom-scrollbar::-webkit-scrollbar-thumb {
              background: rgba(255, 255, 255, 0.15);
              border-radius: 10px;
              border: 2px solid rgba(0, 0, 0, 0.1);
            }
            
            .custom-scrollbar::-webkit-scrollbar-thumb:hover {
              background: rgba(255, 255, 255, 0.25);
            }
            
            /* Firefox için */
            .custom-scrollbar {
              scrollbar-width: thin;
              scrollbar-color: rgba(255, 255, 255, 0.15) rgba(255, 255, 255, 0.05);
            }
          `}</style>
        </>
      )}
    </AnimatePresence>
  );
} 