'use client'

import Link from 'next/link'
import Image from 'next/image'
import { Twitter, Mail, Linkedin, ArrowRight } from 'lucide-react'

// Discord icon (not in lucide-react)
const DiscordIcon = ({ className }: { className?: string }) => (
  <svg className={className} viewBox="0 0 24 24" fill="currentColor">
    <path d="M20.317 4.37a19.791 19.791 0 0 0-4.885-1.515.074.074 0 0 0-.079.037c-.21.375-.444.864-.608 1.25a18.27 18.27 0 0 0-5.487 0 12.64 12.64 0 0 0-.617-1.25.077.077 0 0 0-.079-.037A19.736 19.736 0 0 0 3.677 4.37a.07.07 0 0 0-.032.027C.533 9.046-.32 13.58.099 18.057a.082.082 0 0 0 .031.057 19.9 19.9 0 0 0 5.993 3.03.078.078 0 0 0 .084-.028 14.09 14.09 0 0 0 1.226-1.994.076.076 0 0 0-.041-.106 13.107 13.107 0 0 1-1.872-.892.077.077 0 0 1-.008-.128 10.2 10.2 0 0 0 .372-.292.074.074 0 0 1 .077-.01c3.928 1.793 8.18 1.793 12.062 0a.074.074 0 0 1 .078.01c.12.098.246.198.373.292a.077.077 0 0 1-.006.127 12.299 12.299 0 0 1-1.873.892.077.077 0 0 0-.041.107c.36.698.772 1.362 1.225 1.993a.076.076 0 0 0 .084.028 19.839 19.839 0 0 0 6.002-3.03.077.077 0 0 0 .032-.054c.5-5.177-.838-9.674-3.549-13.66a.061.061 0 0 0-.031-.03zM8.02 15.33c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.956-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.956 2.418-2.157 2.418zm7.975 0c-1.183 0-2.157-1.085-2.157-2.419 0-1.333.955-2.419 2.157-2.419 1.21 0 2.176 1.096 2.157 2.42 0 1.333-.946 2.418-2.157 2.418z"/>
  </svg>
)
import { useStore } from '@/lib/store'
import { api } from '@/lib/api'
import { useCallback, useMemo, useState } from 'react'

export function Footer() {
  const { isAuthenticated } = useStore()
  const [newsletterEmail, setNewsletterEmail] = useState('')
  const [newsletterState, setNewsletterState] = useState<'idle' | 'loading' | 'success' | 'error'>('idle')
  const [newsletterError, setNewsletterError] = useState<string | null>(null)

  const canSubmitNewsletter = useMemo(() => {
    const email = newsletterEmail.trim()
    return email.length > 3 && email.includes('@') && email.includes('.')
  }, [newsletterEmail])

  const handleNewsletterSubmit = useCallback(async (e: React.FormEvent) => {
    e.preventDefault()
    if (!canSubmitNewsletter || newsletterState === 'loading') return

    setNewsletterState('loading')
    setNewsletterError(null)
    try {
      await api.subscribeNewsletter(newsletterEmail.trim())
      setNewsletterState('success')
    } catch (err: any) {
      setNewsletterState('error')
      setNewsletterError(err?.message || 'Subscription failed. Retry.')
    }
  }, [canSubmitNewsletter, newsletterEmail, newsletterState])

  return (
    <footer className="relative border-t border-white/10 bg-[#020202] mt-auto overflow-hidden">
      <div className="absolute inset-0 bg-[url('/noise.png')] opacity-[0.03] mix-blend-overlay pointer-events-none" />
      
      <div className="max-w-[1400px] mx-auto px-4 sm:px-6 py-12 sm:py-20 relative z-10">
        <div className="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-5 gap-8 lg:gap-8 mb-10 sm:mb-16">
          
          {/* Brand Column */}
          <div className="col-span-2 md:col-span-2 lg:col-span-2">
            <div className="mb-4 sm:mb-6">
              <Link href="/" className="inline-block group">
                <div className="flex items-center gap-2 sm:gap-3">
                    <Image 
                        src="/pounce-puma.png" 
                        alt="POUNCE" 
                        width={32} 
                        height={32} 
                        className="object-contain opacity-80 group-hover:opacity-100 transition-opacity sm:w-10 sm:h-10"
                    />
                    <span className="text-lg sm:text-xl font-black tracking-[0.1em] text-white group-hover:text-accent transition-colors font-display">
                        POUNCE
                    </span>
                </div>
              </Link>
            </div>
            <p className="text-xs sm:text-sm font-mono text-white/50 mb-6 sm:mb-8 max-w-sm leading-relaxed">
              High-density domain intelligence for serious investors. Scan. Track. Trade.
            </p>
            
            {/* Newsletter - Hidden on Mobile */}
            <div className="hidden sm:block mb-8 max-w-sm">
                <label className="text-[10px] font-mono uppercase tracking-widest text-white/30 mb-2 block">Mission Briefings</label>
                <form onSubmit={handleNewsletterSubmit} className="flex">
                    <input 
                        type="email" 
                        value={newsletterEmail}
                        onChange={(e) => setNewsletterEmail(e.target.value)}
                        placeholder="Email address"
                        className="w-full bg-[#0A0A0A] border border-white/10 px-4 py-3 text-white font-mono text-xs placeholder:text-white/20 focus:outline-none focus:border-accent transition-all"
                    />
                    <button
                        type="submit"
                        disabled={!canSubmitNewsletter || newsletterState === 'loading'}
                        className="px-4 bg-white text-black hover:bg-accent transition-colors border-l border-white/10 disabled:opacity-50 disabled:cursor-not-allowed"
                        aria-label="Subscribe to briefings"
                        title="Subscribe"
                    >
                        <ArrowRight className="w-4 h-4" />
                    </button>
                </form>
                {newsletterState === 'success' ? (
                  <p className="mt-2 text-[10px] font-mono text-accent/80">Subscribed. Briefings inbound.</p>
                ) : newsletterState === 'error' ? (
                  <p className="mt-2 text-[10px] font-mono text-rose-300/80">{newsletterError}</p>
                ) : (
                  <p className="mt-2 text-[10px] font-mono text-white/20">Weekly intel. No spam.</p>
                )}
            </div>

            <div className="flex items-center gap-3 sm:gap-4">
              <a
                href="https://x.com/pounce_ch"
                target="_blank"
                rel="noopener noreferrer"
                className="w-9 h-9 sm:w-10 sm:h-10 flex items-center justify-center border border-white/5 hover:border-white/20 hover:bg-white/5 transition-all group"
                aria-label="Twitter/X"
              >
                <Twitter className="w-3.5 h-3.5 sm:w-4 sm:h-4 text-white/40 group-hover:text-white transition-colors" />
              </a>
              <a
                href="https://discord.gg/gqyzWMpj8z"
                target="_blank"
                rel="noopener noreferrer"
                className="w-9 h-9 sm:w-10 sm:h-10 flex items-center justify-center border border-white/5 hover:border-white/20 hover:bg-white/5 transition-all group"
                aria-label="Discord"
              >
                <DiscordIcon className="w-3.5 h-3.5 sm:w-4 sm:h-4 text-white/40 group-hover:text-white transition-colors" />
              </a>
              <a
                href="https://www.linkedin.com/company/pounceit"
                target="_blank"
                rel="noopener noreferrer"
                className="w-9 h-9 sm:w-10 sm:h-10 flex items-center justify-center border border-white/5 hover:border-white/20 hover:bg-white/5 transition-all group"
                aria-label="LinkedIn"
              >
                <Linkedin className="w-3.5 h-3.5 sm:w-4 sm:h-4 text-white/40 group-hover:text-white transition-colors" />
              </a>
              <a
                href="mailto:hello@pounce.ch"
                className="w-9 h-9 sm:w-10 sm:h-10 flex items-center justify-center border border-white/5 hover:border-white/20 hover:bg-white/5 transition-all group"
                aria-label="Email"
              >
                <Mail className="w-3.5 h-3.5 sm:w-4 sm:h-4 text-white/40 group-hover:text-white transition-colors" />
              </a>
            </div>
          </div>

          {/* Protocol Links */}
          <div className="col-span-1">
            <h3 className="text-[10px] sm:text-xs font-bold uppercase tracking-[0.15em] sm:tracking-[0.2em] text-white mb-4 sm:mb-6">Protocol</h3>
            <ul className="space-y-3 sm:space-y-4">
              {[
                { href: '/acquire', label: 'Acquire' },
                { href: '/discover', label: 'Discover' },
                { href: '/intelligence', label: 'Intel' },
                { href: '/yield', label: 'Yield' },
                { href: '/pricing', label: 'Pricing' },
              ].map((link) => (
                <li key={link.href}>
                  <Link href={link.href} className="text-xs sm:text-sm font-mono text-white/40 hover:text-accent transition-colors flex items-center gap-1.5 sm:gap-2 group">
                    <span className="w-1 h-1 bg-white/20 group-hover:bg-accent transition-colors" /> {link.label}
                  </Link>
                </li>
              ))}
            </ul>
          </div>

          {/* Info Links */}
          <div className="col-span-1">
            <h3 className="text-[10px] sm:text-xs font-bold uppercase tracking-[0.15em] sm:tracking-[0.2em] text-white mb-4 sm:mb-6">Intel</h3>
            <ul className="space-y-3 sm:space-y-4">
              <li>
                <Link href="/briefings" className="text-xs sm:text-sm font-mono text-white/40 hover:text-white transition-colors">Briefings</Link>
              </li>
              <li>
                <Link href="/about" className="text-xs sm:text-sm font-mono text-white/40 hover:text-white transition-colors">About</Link>
              </li>
              <li>
                <Link href="/contact" className="text-xs sm:text-sm font-mono text-white/40 hover:text-white transition-colors">Contact</Link>
              </li>
              {!isAuthenticated && (
                <li>
                    <Link href="/login" className="text-xs sm:text-sm font-mono text-white/40 hover:text-white transition-colors">Login</Link>
                </li>
              )}
            </ul>
          </div>

          {/* Legal - Hidden on Small Mobile, Merged into Intel on Mobile */}
          <div className="hidden lg:block lg:col-span-1">
            <h3 className="text-xs font-bold uppercase tracking-[0.2em] text-white mb-6">Legal</h3>
            <ul className="space-y-4">
              <li>
                <Link href="/legal/privacy" className="text-sm font-mono text-white/40 hover:text-white transition-colors">Privacy</Link>
              </li>
              <li>
                <Link href="/legal/terms" className="text-sm font-mono text-white/40 hover:text-white transition-colors">Terms</Link>
              </li>
              <li>
                <Link href="/legal/imprint" className="text-sm font-mono text-white/40 hover:text-white transition-colors">Imprint</Link>
              </li>
            </ul>
          </div>
        </div>

        {/* Mobile Legal Links */}
        <div className="lg:hidden flex flex-wrap gap-4 mb-8 text-[10px] font-mono text-white/30">
          <Link href="/legal/privacy" className="hover:text-white transition-colors">Privacy</Link>
          <span className="text-white/10">|</span>
          <Link href="/legal/terms" className="hover:text-white transition-colors">Terms</Link>
          <span className="text-white/10">|</span>
          <Link href="/legal/imprint" className="hover:text-white transition-colors">Imprint</Link>
        </div>

        {/* Bottom Bar */}
        <div className="pt-6 sm:pt-8 border-t border-white/10 flex flex-col sm:flex-row justify-between items-center gap-3 sm:gap-4 text-[9px] sm:text-[10px] font-mono text-white/20 uppercase tracking-widest">
          <p>© {new Date().getFullYear()} Gugger Digital Services — WÄDENSWIL</p>
          <div className="flex items-center gap-4 sm:gap-6">
            <span className="flex items-center gap-1.5">
              <span className="w-1.5 h-1.5 bg-accent animate-pulse" />
              Online
            </span>
            <span>v2.0.4</span>
          </div>
        </div>
      </div>
    </footer>
  )
}
