// Modal de vídeo em tela cheia (reel de apresentação, YouTube com autoplay).
// Padrão igual ao ContactModal: monta uma vez na composição, abre via
// window.openVideoModal(videoId opcional — usa DEFAULT_VIDEO_ID se omitido).
// youtube-nocookie.com = modo privacidade avançada (sem cookie de rastreio
// antes do consentimento).
const DEFAULT_VIDEO_ID = 'NO_uaSGueIM';

function VideoModal() {
  const [videoId, setVideoId] = React.useState(null);

  React.useEffect(() => {
    window.openVideoModal = (id) => setVideoId(id || DEFAULT_VIDEO_ID);
    return () => { delete window.openVideoModal; };
  }, []);

  React.useEffect(() => {
    if (!videoId) return;
    document.body.style.overflow = 'hidden';
    const onKey = (e) => { if (e.key === 'Escape') setVideoId(null); };
    document.addEventListener('keydown', onKey);
    return () => {
      document.body.style.overflow = '';
      document.removeEventListener('keydown', onKey);
    };
  }, [videoId]);

  if (!videoId) return null;

  return (
    <div
      onClick={(e) => { if (e.target === e.currentTarget) setVideoId(null); }}
      style={{
        position: 'fixed', inset: 0, zIndex: 300,
        background: 'rgba(10, 8, 12, 0.92)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        padding: '64px 20px 20px',
      }}
    >
      <button
        onClick={() => setVideoId(null)}
        aria-label="Fechar vídeo"
        style={{
          position: 'fixed', top: 18, right: 18, zIndex: 301,
          width: 48, height: 48, borderRadius: '50%',
          background: 'rgba(255,255,255,0.12)',
          border: '1.5px solid rgba(255,255,255,0.35)',
          color: '#fff',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          cursor: 'pointer',
        }}
      >
        <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.3" strokeLinecap="round" aria-hidden="true">
          <path d="M6 6l12 12M18 6l-12 12" />
        </svg>
      </button>

      <div style={{ width: '100%', maxWidth: 960, aspectRatio: '16 / 9', position: 'relative' }}>
        <iframe
          src={`https://www.youtube-nocookie.com/embed/${videoId}?autoplay=1&rel=0&modestbranding=1`}
          title="Econome — vídeo de apresentação"
          allow="autoplay; encrypted-media; picture-in-picture"
          allowFullScreen
          style={{ width: '100%', height: '100%', border: 0, borderRadius: 12 }}
        />
      </div>
    </div>
  );
}
window.VideoModal = VideoModal;
