// ── TEXT SCRAMBLE ─────────────────────────────────────────
const SCRAMBLE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*';

function TextScramble({ text, className }) {
  const [displayText, setDisplayText] = useState(text);
  const [isHovering, setIsHovering] = useState(false);
  const [isScrambling, setIsScrambling] = useState(false);
  const intervalRef = useRef(null);
  const frameRef = useRef(0);

  const scramble = React.useCallback(() => {
    setIsScrambling(true);
    frameRef.current = 0;
    const duration = text.length * 3;
    if (intervalRef.current) clearInterval(intervalRef.current);

    intervalRef.current = setInterval(() => {
      frameRef.current++;
      const progress = frameRef.current / duration;
      const revealedLength = Math.floor(progress * text.length);

      const newText = text
        .split('')
        .map((char, i) => {
          if (char === ' ' || char === '@' || char === '.') return char;
          if (i < revealedLength) return text[i];
          return SCRAMBLE_CHARS[Math.floor(Math.random() * SCRAMBLE_CHARS.length)];
        })
        .join('');

      setDisplayText(newText);

      if (frameRef.current >= duration) {
        clearInterval(intervalRef.current);
        setDisplayText(text);
        setIsScrambling(false);
      }
    }, 30);
  }, [text]);

  useEffect(() => {
    return () => { if (intervalRef.current) clearInterval(intervalRef.current); };
  }, []);

  return (
    <div
      className={`footer-scramble${className ? ' ' + className : ''}`}
      onMouseEnter={() => { setIsHovering(true); scramble(); }}
      onMouseLeave={() => setIsHovering(false)}
    >
      <span className="footer-scramble-chars">
        {displayText.split('').map((char, i) => (
          <span
            key={i}
            className={`footer-scramble-char${isScrambling && char !== text[i] ? ' active' : ''}`}
            style={{ transitionDelay: `${i * 8}ms` }}
          >
            {char}
          </span>
        ))}
      </span>
      <span className="footer-scramble-line">
        <span className={`footer-scramble-line-fill${isHovering ? ' visible' : ''}`} />
        <span className="footer-scramble-line-base" />
      </span>
    </div>
  );
}

// ── FOOTER ────────────────────────────────────────────────
function Footer() {
  const NAV_LINKS = [
    { label: 'Nosotros',  href: '#bivia' },
    { label: 'Servicios', href: '#servicios' },
    { label: 'Proyectos', href: '#proyectos' },
    { label: 'Contacto',  href: '#cta' },
  ];

  const SOCIAL_LINKS = [
    { label: 'LinkedIn',  href: 'https://linkedin.com/company/bivia' },
    { label: 'Instagram', href: 'https://instagram.com/bivia.mx' },
  ];

  return (
    <footer id="footer" className="footer-root">
      <div className="footer-bg" />

      {/* 3-column content grid */}
      <div className="footer-grid">
        {/* Brand */}
        <div className="footer-col">
          <img
            src="Branding/Bivia_logo_blanco_png.png"
            alt="BIVIA"
            className="footer-logo-img"
          />
          <p className="footer-brand-tagline">Sistemas · Estrategia · Ejecución</p>
          <p className="footer-brand-desc">
            Construimos sistemas digitales que ordenan, automatizan y escalan operaciones de negocio.
          </p>
        </div>

        {/* Navigation */}
        <div className="footer-col">
          <p className="footer-col-heading">Navegación</p>
          <div className="footer-nav-list">
            {NAV_LINKS.map(({ label, href }) => (
              <a key={label} href={href} className="footer-nav-item">
                <TextScramble text={label} />
              </a>
            ))}
          </div>
        </div>

        {/* Contact */}
        <div className="footer-col">
          <p className="footer-col-heading">Contacto</p>
          <div className="footer-contact-list">
            <a href="mailto:hola@bivia.mx" className="footer-contact-item">
              <TextScramble text="hola@bivia.mx" />
            </a>
            <div className="footer-social-list">
              {SOCIAL_LINKS.map(({ label, href }) => (
                <a
                  key={label}
                  href={href}
                  target="_blank"
                  rel="noopener noreferrer"
                  className="footer-contact-item"
                >
                  <TextScramble text={label} />
                </a>
              ))}
            </div>
          </div>
        </div>
      </div>

      {/* Bottom bar */}
      <div className="footer-bottom">
        <span className="footer-copy">© 2025 BIVIA. Todos los derechos reservados.</span>
        <span className="footer-copy">Ciudad de México</span>
      </div>
    </footer>
  );
}
