// ImpactSection.jsx + ManifestoSection.jsx + FooterCTA.jsx

// Animated counter hook
const useCountUp = (end, duration = 2000, startOnVisible = true) => {
  const [count, setCount] = React.useState(0);
  const [started, setStarted] = React.useState(false);
  const ref = React.useRef(null);

  React.useEffect(() => {
    if (!startOnVisible) { setStarted(true); return; }
    const obs = new IntersectionObserver(
      ([entry]) => { if (entry.isIntersecting) { setStarted(true); obs.disconnect(); } },
      { threshold: 0.3 }
    );
    if (ref.current) obs.observe(ref.current);
    return () => obs.disconnect();
  }, []);

  React.useEffect(() => {
    if (!started) return;
    let startTime = null;
    const animate = (timestamp) => {
      if (!startTime) startTime = timestamp;
      const progress = Math.min((timestamp - startTime) / duration, 1);
      // Ease out cubic
      const eased = 1 - Math.pow(1 - progress, 3);
      setCount(Math.round(eased * end));
      if (progress < 1) requestAnimationFrame(animate);
    };
    requestAnimationFrame(animate);
  }, [started, end, duration]);

  return { count, ref };
};

const ImpactSection = () => {
  // Placeholder stats — user will provide real numbers later
  const stats = [
    { num: "50%+", label: "Reduction in cost-per-hire", countTo: 50, suffix: '%+' },
    { num: "100s of hours", label: "Saved per hire cycle", countTo: 100, suffix: 's of hours' },
    { num: "Zero", label: "Interviewer bias in AI screening layer", countTo: 0, suffix: '', isWord: true, word: 'Zero' },
  ];

  return (
    <section id="impact-section" style={{ background: '#0F0D07', padding: '120px 48px', position: 'relative', overflow: 'hidden' }}>
      <div style={{ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%,-50%)', width: 700, height: 400, background: 'radial-gradient(ellipse, rgba(201,168,76,0.06) 0%, transparent 70%)', pointerEvents: 'none' }}/>
      <div style={{ maxWidth: 1200, margin: '0 auto', position: 'relative' }}>
        <Eyebrow>What We Solve</Eyebrow>
        <h2 style={{ fontFamily: 'Space Grotesk, sans-serif', fontSize: 'clamp(2rem, 4vw, 2.8rem)', fontWeight: 700, color: '#F5F0E8', letterSpacing: '-0.02em', lineHeight: 1.15, marginBottom: 64, maxWidth: 560 }}>
          What changes when you use intervieHire.
        </h2>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(240px, 1fr))', gap: 0, marginBottom: 56 }}>
          {stats.map((s, i) => {
            const counter = useCountUp(s.countTo, 2000);
            return (
              <RevealCard key={i} delay={i * 0.1}>
                <div ref={counter.ref} style={{ padding: '0 40px', borderRight: i < 2 ? '1px solid rgba(201,168,76,0.1)' : 'none', paddingLeft: i === 0 ? 0 : 40 }}>
                  <div style={{ fontFamily: 'Space Grotesk, sans-serif', fontSize: 'clamp(2.5rem, 5vw, 3.5rem)', fontWeight: 700, letterSpacing: '-0.02em', lineHeight: 1, marginBottom: 12, background: 'linear-gradient(135deg, #C9A84C, #E8C97A, #C9A84C)', WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent', backgroundClip: 'text' }}>
                    {s.isWord ? s.word : `${counter.count}${s.suffix}`}
                  </div>
                  <div style={{ fontFamily: 'Outfit, sans-serif', fontSize: 15, color: '#888880', lineHeight: 1.5 }}>{s.label}</div>
                </div>
              </RevealCard>
            );
          })}
        </div>
        <RevealCard delay={0.3}>
          <p style={{ fontFamily: 'Outfit, sans-serif', fontSize: 16, color: '#555550', lineHeight: 1.7, maxWidth: 680, borderTop: '1px solid rgba(201,168,76,0.1)', paddingTop: 40 }}>
            Our interviewers are trained, calibrated, and use a standardized framework, removing bias and ensuring every candidate gets a fair, high-quality assessment.
          </p>
        </RevealCard>
      </div>
    </section>
  );
};

const ManifestoSection = () => {
  const words = ["We", "didn't", "build", "another", "tool.", "We", "built", "the", "hiring", "layer", "your", "team", "never", "had."];
  return (
    <section style={{ minHeight: '100vh', background: '#000', display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '120px 48px', position: 'relative', overflow: 'hidden' }}>
      <div style={{ position: 'absolute', inset: 0, opacity: 0.08 }}>
        <GoldWaveSVG style={{ width: '100%', height: '100%' }} />
      </div>
      <RevealCard delay={0}>
        <blockquote style={{ maxWidth: 860, textAlign: 'center', position: 'relative', zIndex: 1 }}>
          <p style={{ fontFamily: 'Space Grotesk, sans-serif', fontStyle: 'italic', fontSize: 'clamp(1.6rem, 4vw, 2.8rem)', fontWeight: 700, color: '#C9A84C', letterSpacing: '-0.02em', lineHeight: 1.3 }}>
            "{words.join(' ')}"
          </p>
        </blockquote>
      </RevealCard>
    </section>
  );
};

const FooterCTA = () => {
  const [email, setEmail] = React.useState('');
  const [waitlistStatus, setWaitlistStatus] = React.useState('idle'); // idle | submitting | success | error
  const [emailError, setEmailError] = React.useState('');

  const handleWaitlist = async (e) => {
    e.preventDefault();
    setEmailError('');

    if (!email.trim()) {
      setEmailError('Please enter your email');
      return;
    }
    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
      setEmailError('Please enter a valid work email');
      return;
    }

    setWaitlistStatus('submitting');
    await new Promise(r => setTimeout(r, 1000));

    // Store in localStorage
    const waitlist = JSON.parse(localStorage.getItem('intervieHire_waitlist') || '[]');
    waitlist.push({ email, timestamp: new Date().toISOString(), type: 'waitlist' });
    localStorage.setItem('intervieHire_waitlist', JSON.stringify(waitlist));

    setWaitlistStatus('success');
    setTimeout(() => {
      setWaitlistStatus('idle');
      setEmail('');
    }, 3000);
  };

  // Placeholder URLs — user will provide real ones
  const socialLinks = {
    LinkedIn: '#linkedin',
    Twitter: '#twitter',
  };
  const websiteUrl = '#';
  const contactEmail = 'interviehire@gmail.com';

  return (
    <footer id="footer-section" style={{ background: '#0A0A0A', padding: '120px 48px 60px', position: 'relative', overflow: 'hidden' }}>
      <div style={{ position: 'absolute', right: 0, top: 0, width: '45%', height: '100%', opacity: 0.5, pointerEvents: 'none' }}>
        <GoldWaveSVG style={{ width: '100%', height: '100%' }}/>
      </div>
      <div style={{ maxWidth: 1200, margin: '0 auto', position: 'relative' }}>
        {/* CTA */}
        <div style={{ maxWidth: 640, marginBottom: 80 }}>
          <h2 style={{ fontFamily: 'Space Grotesk, sans-serif', fontSize: 'clamp(2.2rem, 5vw, 3.5rem)', fontWeight: 700, color: '#F5F0E8', letterSpacing: '-0.02em', lineHeight: 1.1, marginBottom: 20 }}>
            Ready to hire smarter?
          </h2>
          <p style={{ fontFamily: 'Outfit, sans-serif', fontSize: 17, color: '#888880', lineHeight: 1.65, marginBottom: 40 }}>
            Join the companies already running interviews 24/7, without burning out their teams.
          </p>

          {waitlistStatus === 'success' ? (
            <div style={{
              display: 'flex', alignItems: 'center', gap: 12,
              padding: '14px 24px', background: 'rgba(201,168,76,0.08)',
              border: '1px solid rgba(201,168,76,0.2)', borderRadius: 9999,
              maxWidth: 480,
            }}>
              <span style={{ fontSize: 20 }}>✓</span>
              <span style={{ fontFamily: 'Outfit, sans-serif', fontSize: 14, color: '#C9A84C', fontWeight: 500 }}>
                You're on the list! We'll be in touch.
              </span>
            </div>
          ) : (
            <form onSubmit={handleWaitlist}>
              <div style={{ display: 'flex', gap: 10, maxWidth: 480 }}>
                <div style={{ flex: 1, position: 'relative' }}>
                  <input
                    type="email"
                    placeholder="Enter your work email"
                    value={email}
                    onChange={e => { setEmail(e.target.value); setEmailError(''); }}
                    onFocus={e => { e.target.style.borderColor = 'rgba(201,168,76,0.5)'; e.target.style.boxShadow = '0 0 0 3px rgba(201,168,76,0.06)'; }}
                    onBlur={e => { e.target.style.borderColor = emailError ? '#ff4444' : 'rgba(201,168,76,0.2)'; e.target.style.boxShadow = 'none'; }}
                    style={{
                      width: '100%', fontFamily: 'Outfit, sans-serif', fontSize: 14,
                      background: 'rgba(255,255,255,0.03)',
                      border: `1px solid ${emailError ? '#ff4444' : 'rgba(201,168,76,0.2)'}`,
                      borderRadius: 9999, padding: '12px 20px', color: '#F5F0E8', outline: 'none',
                      transition: 'border-color 0.2s, box-shadow 0.2s',
                    }}
                  />
                </div>
                <button
                  type="submit"
                  disabled={waitlistStatus === 'submitting'}
                  style={{
                    fontFamily: 'Outfit, sans-serif', fontSize: 14, fontWeight: 500,
                    background: 'linear-gradient(90deg,#FF6B35,#E91E8C)', color: '#fff',
                    border: 'none', borderRadius: 9999, padding: '12px 24px',
                    cursor: waitlistStatus === 'submitting' ? 'wait' : 'pointer',
                    whiteSpace: 'nowrap',
                    opacity: waitlistStatus === 'submitting' ? 0.7 : 1,
                    transition: 'filter 0.2s, transform 0.15s',
                  }}
                  onMouseEnter={e => { if (waitlistStatus !== 'submitting') { e.currentTarget.style.filter = 'brightness(1.12)'; e.currentTarget.style.transform = 'translateY(-1px)'; }}}
                  onMouseLeave={e => { e.currentTarget.style.filter = 'none'; e.currentTarget.style.transform = 'translateY(0)'; }}
                >
                  {waitlistStatus === 'submitting' ? 'Joining...' : 'Join Waitlist'}
                </button>
              </div>
              {emailError && (
                <div style={{ fontFamily: 'Outfit, sans-serif', fontSize: 12, color: '#ff4444', marginTop: 8, paddingLeft: 4 }}>
                  {emailError}
                </div>
              )}
            </form>
          )}
        </div>

        {/* Divider */}
        <div style={{ borderTop: '1px solid rgba(201,168,76,0.1)', paddingTop: 48 }}>
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', flexWrap: 'wrap', gap: 32 }}>
            <div>
              <Logo size={20}/>
              <div style={{ fontFamily: 'Outfit, sans-serif', fontSize: 13, color: '#555550', marginTop: 10, letterSpacing: '0.04em' }}>Autonomous Interviews. Human Results.</div>
              <div style={{ marginTop: 16, display: 'flex', gap: 16 }}>
                {Object.entries(socialLinks).map(([name, url]) => (
                  <a key={name} href={url} target="_blank" rel="noopener noreferrer"
                    style={{ fontFamily: 'Outfit, sans-serif', fontSize: 13, color: '#888880', textDecoration: 'none', transition: 'color 0.2s', display: 'inline-flex', alignItems: 'center', gap: 6 }}
                    onMouseEnter={e => e.currentTarget.style.color = '#C9A84C'}
                    onMouseLeave={e => e.currentTarget.style.color = '#888880'}
                  >
                    {name === 'LinkedIn' && (
                      <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M19 3a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h14m-.5 15.5v-5.3a3.26 3.26 0 0 0-3.26-3.26c-.85 0-1.84.52-2.32 1.3v-1.11h-2.79v8.37h2.79v-4.93c0-.77.62-1.4 1.39-1.4a1.4 1.4 0 0 1 1.4 1.4v4.93h2.79M6.88 8.56a1.68 1.68 0 0 0 1.68-1.68c0-.93-.75-1.69-1.68-1.69a1.69 1.69 0 0 0-1.69 1.69c0 .93.76 1.68 1.69 1.68m1.39 9.94v-8.37H5.5v8.37h2.77z"/></svg>
                    )}
                    {name === 'Twitter' && (
                      <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
                    )}
                    {name}
                  </a>
                ))}
              </div>
            </div>
            <div style={{ textAlign: 'right' }}>
              <a href={websiteUrl} target="_blank" rel="noopener noreferrer"
                style={{ fontFamily: 'Outfit, sans-serif', fontSize: 13, color: '#555550', textDecoration: 'none', display: 'block', marginBottom: 6, transition: 'color 0.2s' }}
                onMouseEnter={e => e.currentTarget.style.color = '#C9A84C'}
                onMouseLeave={e => e.currentTarget.style.color = '#555550'}
              >interviehire.com</a>
              <a href={`mailto:${contactEmail}`}
                style={{ fontFamily: 'Outfit, sans-serif', fontSize: 13, color: '#555550', textDecoration: 'none', display: 'block', marginBottom: 6, transition: 'color 0.2s' }}
                onMouseEnter={e => e.currentTarget.style.color = '#C9A84C'}
                onMouseLeave={e => e.currentTarget.style.color = '#555550'}
              >{contactEmail}</a>
              <div style={{ fontFamily: 'Outfit, sans-serif', fontSize: 12, color: '#444440', marginTop: 16 }}>© 2025 intervieHire. All rights reserved.</div>
            </div>
          </div>
        </div>
      </div>
    </footer>
  );
};

Object.assign(window, { ImpactSection, ManifestoSection, FooterCTA });
