'use client'

import { useState, FormEvent } from 'react'
import ScrollReveal from '@/components/ui/ScrollReveal'

const FLOWFI_LIST_UUID = 'fefebe2c-0966-4df5-81ec-1eae9b9dce3f'
const SUBSCRIBE_URL = 'https://newsletter.jeffemmett.com/subscribe'

export default function CTASection() {
  const [email, setEmail] = useState('')
  const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle')

  async function handleSubmit(e: FormEvent) {
    e.preventDefault()
    if (!email) return

    setStatus('loading')
    try {
      const res = await fetch(SUBSCRIBE_URL, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ email, list_uuid: FLOWFI_LIST_UUID }),
      })
      if (res.ok) {
        setStatus('success')
        setEmail('')
      } else {
        setStatus('error')
      }
    } catch {
      setStatus('error')
    }
  }

  return (
    <section className="min-h-[60vh] flex flex-col items-center justify-center relative px-4 py-24">
      <div className="absolute inset-0 bg-gradient-to-b from-deep to-ocean" />

      {/* Soft glow behind CTA */}
      <div
        className="absolute top-1/3 left-1/2 -translate-x-1/2 w-[400px] h-[400px] rounded-full"
        style={{ background: 'radial-gradient(circle, rgba(45, 212, 191, 0.06) 0%, transparent 70%)' }}
      />

      <div className="relative z-10 text-center max-w-3xl">
        <ScrollReveal>
          <h2 className="font-sans font-extralight text-4xl md:text-6xl text-foam/80 mb-5 tracking-tight">
            Ready to <span className="text-flow">flow</span>?
          </h2>
        </ScrollReveal>

        <ScrollReveal delay={0.2}>
          <p className="font-caveat text-2xl text-foam/35 mb-10">
            Dive into the current.
          </p>
        </ScrollReveal>

        <ScrollReveal delay={0.3}>
          {status === 'success' ? (
            <p className="text-flow/70 text-sm font-light mb-10">
              You&apos;re in the current now. Welcome.
            </p>
          ) : (
            <form onSubmit={handleSubmit} className="flex flex-col sm:flex-row gap-3 justify-center items-center mb-10 max-w-md mx-auto">
              <input
                type="email"
                value={email}
                onChange={e => setEmail(e.target.value)}
                placeholder="your@email.com"
                required
                className="w-full sm:flex-1 bg-ocean/40 border border-flow/15 rounded-full px-6 py-3 text-foam/70 placeholder:text-mist/25 text-sm font-light focus:outline-none focus:border-flow/40 focus:bg-ocean/60 transition-all duration-500"
              />
              <button
                type="submit"
                disabled={status === 'loading'}
                className="border border-flow/25 px-8 py-3 text-foam/50 hover:text-flow/80 hover:border-flow/50 hover:bg-flow/5 transition-all duration-500 text-sm rounded-full font-light disabled:opacity-40 whitespace-nowrap"
              >
                {status === 'loading' ? 'Diving in...' : 'Subscribe'}
              </button>
            </form>
          )}
          {status === 'error' && (
            <p className="text-anti-green/50 text-xs font-light mb-6">
              Something went wrong. Try again?
            </p>
          )}
        </ScrollReveal>

        <ScrollReveal delay={0.4}>
          <p className="text-mist/25 text-sm mb-10 tracking-wide font-light">
            NoFi → FlowFi → ???
          </p>
        </ScrollReveal>

        <ScrollReveal delay={0.5}>
          <a
            href="https://nofi.lol"
            className="inline-block border border-flow/15 px-10 py-4 text-foam/30 hover:text-flow/60 hover:border-flow/40 hover:bg-flow/5 transition-all duration-700 text-sm rounded-full font-light"
          >
            ← back to NoFi
          </a>
        </ScrollReveal>
      </div>

      {/* Footer — clean, minimal */}
      <footer className="relative z-10 w-full mt-24 pt-6 flex justify-between items-center max-w-4xl"
        style={{ borderTop: '1px solid rgba(45, 212, 191, 0.08)' }}
      >
        <span className="text-mist/20 text-xs font-light">
          Part of the CoFi cinematic universe
        </span>
        <a
          href="https://nofi.lol"
          className="text-mist/20 text-xs font-light hover:text-flow/40 transition-all duration-700"
        >
          nofi.lol
        </a>
      </footer>
    </section>
  )
}
