'use client'

import { useState, useEffect } from 'react'
import Image from 'next/image'
import Link from 'next/link'
import { Header } from '@/components/Header'
import { Footer } from '@/components/Footer'
import { useStore } from '@/lib/store'
import {
  Coins,
  TrendingUp,
  Shield,
  Target,
  ArrowRight,
  Check,
  Zap,
  BarChart3,
  Server,
  Network,
  Globe,
  Lock,
  ArrowUpRight,
  Cpu,
  Share2,
  Wallet,
  PieChart,
  RefreshCw,
  Play,
  Calculator,
  Info
} from 'lucide-react'
import clsx from 'clsx'

// Yield Simulator Component - CONSERVATIVE VALUES
function YieldSimulator() {
  const [traffic, setTraffic] = useState(1000)
  const [vertical, setVertical] = useState('finance')

  // Vertical data - CONSERVATIVE conversion rates and CPAs
  // These are industry-realistic values for generic domain traffic
  const verticals: Record<string, { cpa: number, conv: number, label: string }> = {
    finance: { cpa: 25, conv: 0.8, label: 'Finance & Loans' },
    insurance: { cpa: 18, conv: 0.6, label: 'Insurance' },
    legal: { cpa: 35, conv: 0.4, label: 'Legal Services' },
    medical: { cpa: 15, conv: 1.0, label: 'Medical / Health' },
    tech: { cpa: 12, conv: 0.5, label: 'Software / B2B' }
  }

  const currentVertical = verticals[vertical]
  const conversions = Math.floor(traffic * (currentVertical.conv / 100))
  const monthlyRevenue = conversions * currentVertical.cpa
  const annualYield = monthlyRevenue * 12

  return (
    <div className="relative group max-w-md mx-auto lg:ml-auto lg:mr-0">
      {/* Ambient Glow - White/Green instead of Amber */}
      <div className="absolute -inset-0.5 bg-gradient-to-br from-accent/20 via-white/5 to-accent/20 blur-2xl opacity-30 group-hover:opacity-50 transition-opacity duration-1000" />
      
      {/* Main Container - Compact Tech Card */}
      <div className="relative bg-[#050505]/90 backdrop-blur-xl border border-white/10 shadow-2xl overflow-hidden">
        {/* Decorative Tech Elements */}
        <div className="absolute top-0 right-0 p-3">
            <div className="flex gap-1.5">
                <div className="w-1 h-1 bg-white/20 rounded-full" />
                <div className="w-1 h-1 bg-accent animate-pulse rounded-full" />
            </div>
        </div>

        <div className="p-6 sm:p-8">
           <div className="mb-6">
              <h3 className="font-display text-2xl text-white mb-1">Yield Projection</h3>
              <p className="font-mono text-[10px] text-white/40 uppercase tracking-widest">Est. Passive Income</p>
           </div>

           {/* Input 1: Traffic Slider */}
           <div className="mb-6">
              <div className="flex justify-between text-xs font-mono mb-3">
                 <span className="text-white/60">Monthly Traffic</span>
                 <span className="text-white">{traffic.toLocaleString()} Visits</span>
              </div>
              <div className="relative h-6 flex items-center">
                 <input 
                    type="range" 
                    min="100" 
                    max="5000" 
                    step="100" 
                    value={traffic} 
                    onChange={(e) => setTraffic(parseInt(e.target.value))}
                    className="w-full h-2 bg-white/10 appearance-none cursor-pointer
                               [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-4 [&::-webkit-slider-thumb]:h-4 
                               [&::-webkit-slider-thumb]:bg-accent [&::-webkit-slider-thumb]:border-2 [&::-webkit-slider-thumb]:border-white
                               [&::-webkit-slider-thumb]:cursor-pointer
                               [&::-moz-range-thumb]:w-4 [&::-moz-range-thumb]:h-4 
                               [&::-moz-range-thumb]:bg-accent [&::-moz-range-thumb]:border-2 [&::-moz-range-thumb]:border-white
                               [&::-moz-range-thumb]:cursor-pointer [&::-moz-range-thumb]:rounded-none"
                    style={{
                      background: `linear-gradient(to right, rgb(16 185 129) 0%, rgb(16 185 129) ${((traffic - 100) / (5000 - 100)) * 100}%, rgba(255,255,255,0.1) ${((traffic - 100) / (5000 - 100)) * 100}%, rgba(255,255,255,0.1) 100%)`
                    }}
                 />
              </div>
              <div className="flex justify-between text-[10px] font-mono text-white/30 mt-1">
                 <span>100</span>
                 <span>5,000</span>
              </div>
           </div>

           {/* Input 2: Vertical Selector (Grid) */}
           <div className="mb-8">
              <label className="block text-xs font-mono text-white/60 mb-3">Domain Vertical</label>
              <div className="grid grid-cols-2 gap-2">
                 {Object.entries(verticals).map(([key, data]) => (
                    <button
                        key={key}
                        onClick={() => setVertical(key)}
                        className={clsx(
                            "px-3 py-2 text-[10px] font-bold uppercase tracking-wide text-left transition-all border",
                            vertical === key 
                                ? "bg-white text-black border-white" 
                                : "bg-white/5 text-white/40 border-transparent hover:text-white hover:bg-white/10"
                        )}
                    >
                        {data.label}
                    </button>
                 ))}
              </div>
           </div>

           {/* Result Area */}
           <div className="bg-white/[0.03] border border-white/5 p-5 relative overflow-hidden">
              <div className="relative z-10">
                 <div className="flex justify-between items-end mb-3">
                    <span className="text-[10px] font-mono uppercase tracking-widest text-white/40">Est. Annual</span>
                    <div className="text-right">
                       <span className="block font-display text-3xl text-white tracking-tight leading-none">${annualYield.toLocaleString('en-US')}</span>
                    </div>
                 </div>
                 
                 {/* Conversion breakdown */}
                 <div className="bg-white/[0.02] border border-white/5 p-3 mb-3">
                    <div className="flex justify-between text-[10px] font-mono mb-1">
                       <span className="text-white/40">Visitors</span>
                       <span className="text-white">{traffic.toLocaleString()}</span>
                    </div>
                    <div className="flex justify-between text-[10px] font-mono mb-1">
                       <span className="text-white/40">Conv. Rate</span>
                       <span className="text-accent">{currentVertical.conv}%</span>
                    </div>
                    <div className="flex justify-between text-[10px] font-mono">
                       <span className="text-white/40">Conversions/mo</span>
                       <span className="text-white">{conversions}</span>
                    </div>
                 </div>
                 
                 <div className="flex justify-between items-center pt-3 border-t border-white/5 text-[10px] font-mono text-white/30">
                    <span>${monthlyRevenue.toLocaleString('en-US')} / mo</span>
                    <span>CPA: ${currentVertical.cpa}</span>
                 </div>
              </div>
           </div>
        </div>
        
        {/* Footer Bar */}
        <div className="bg-[#080808] px-6 py-3 border-t border-white/5 flex justify-between items-center text-[9px] font-mono text-white/20 uppercase tracking-wider">
            <span>Live Calculation</span>
            <span>Fee: 0%</span>
        </div>
      </div>
    </div>
  )
}

export default function YieldPage() {
  const { isAuthenticated, checkAuth } = useStore()

  useEffect(() => {
    checkAuth()
  }, [checkAuth])

  return (
    <div className="min-h-screen bg-[#020202] text-white relative overflow-x-hidden selection:bg-accent/30 selection:text-white">
      {/* Cinematic Background */}
      <div className="fixed inset-0 pointer-events-none z-0">
        <div className="absolute inset-0 bg-[url('/noise.png')] opacity-[0.03] mix-blend-overlay" />
        <div 
          className="absolute inset-0 opacity-[0.03]"
          style={{
            backgroundImage: `linear-gradient(rgba(255,255,255,0.3) 0.5px, transparent 0.5px), linear-gradient(90deg, rgba(255,255,255,0.3) 0.5px, transparent 0.5px)`,
            backgroundSize: '160px 160px',
          }}
        />
        {/* Gold/Amber Glow for Yield context instead of Green */}
        <div className="absolute top-[20%] right-[-10%] w-[800px] h-[800px] bg-amber-500/[0.02] rounded-full blur-[150px]" />
      </div>

      <Header />

      {/* HERO SECTION */}
      <section className="relative min-h-[85vh] sm:min-h-[90vh] flex flex-col justify-center pt-20 sm:pt-32 pb-12 sm:pb-24 px-4 sm:px-6 z-10">
        <div className="max-w-[1400px] mx-auto w-full">
          <div className="grid lg:grid-cols-12 gap-8 lg:gap-24 items-center">
            
            {/* Left: Typography */}
            <div className="lg:col-span-6 relative z-20 text-left">
              <div className="inline-flex items-center gap-3 sm:gap-4 mb-6 sm:mb-10 animate-fade-in opacity-0" style={{ animationDelay: '0.1s', animationFillMode: 'forwards' }}>
                <div className="h-px w-6 sm:w-12 bg-white/10" />
                <span className="text-[9px] sm:text-[10px] font-mono uppercase tracking-[0.2em] sm:tracking-[0.3em] text-accent">Pounce // Yield</span>
              </div>

              <h1 className="font-display text-[2.5rem] sm:text-[4rem] md:text-[5rem] lg:text-[6rem] leading-[0.9] tracking-[-0.03em] text-white mb-6 sm:mb-8">
                <span className="block animate-slide-up opacity-0" style={{ animationDelay: '0.2s', animationFillMode: 'forwards' }}>
                  Assets that
                </span>
                <span className="block text-white animate-slide-up opacity-0" style={{ animationDelay: '0.4s', animationFillMode: 'forwards' }}>
                  work for you.
                </span>
              </h1>
              
              <div className="animate-slide-up opacity-0" style={{ animationDelay: '0.8s', animationFillMode: 'forwards' }}>
                <p className="text-sm sm:text-lg lg:text-xl text-white/60 max-w-xl font-light leading-relaxed mb-8 sm:mb-12">
                  Turn dormant domains into active cashflow. Our Intent Routing™ engine directs traffic to high-paying partners.
                </p>
                
                <div className="flex flex-col sm:flex-row items-start gap-4 sm:gap-6">
                    <Link
                        href={isAuthenticated ? '/terminal/yield' : '/register'}
                        className="px-8 py-4 bg-accent text-black text-xs font-bold uppercase tracking-[0.2em] hover:bg-white transition-all shadow-[0_0_20px_rgba(16,185,129,0.2)]"
                        style={{ clipPath: 'polygon(10px 0, 100% 0, 100% 100%, 0 100%, 0 10px)' }}
                    >
                        Activate Protocol
                    </Link>
                    <button 
                        onClick={() => document.getElementById('how-it-works')?.scrollIntoView({ behavior: 'smooth' })}
                        className="text-xs font-bold uppercase tracking-[0.2em] text-white/40 hover:text-white flex items-center gap-2"
                    >
                        <Play className="w-3 h-3" /> How It Works
                    </button>
                </div>
              </div>
            </div>

            {/* Right: Simulator */}
            <div className="lg:col-span-6 relative animate-scale-in opacity-0 mt-8 lg:mt-0" style={{ animationDelay: '1s', animationFillMode: 'forwards' }}>
               <YieldSimulator />
            </div>
          </div>
        </div>
      </section>

      {/* CORE LOGIC */}
      <section id="how-it-works" className="relative py-20 sm:py-40 px-4 sm:px-6 bg-[#020202]">
        <div className="max-w-[1400px] mx-auto">
          {/* Section Header - Tech Style */}
          <div className="flex flex-col lg:flex-row justify-between items-start lg:items-end mb-12 sm:mb-24 border-b border-white/[0.08] pb-8 sm:pb-12">
            <div>
              <span className="text-accent font-mono text-[10px] sm:text-xs uppercase tracking-[0.2em] mb-3 sm:mb-4 block">The Mechanism</span>
              <h2 className="font-display text-2xl sm:text-4xl lg:text-5xl text-white leading-none">
                Intent Routing™
              </h2>
            </div>
            <p className="hidden lg:block text-white/50 max-w-md text-sm font-mono mt-8 lg:mt-0 leading-relaxed lg:text-right">
              // REPLACING_PARKING_PAGES<br />
              // ELIMINATING_MIDDLEMEN<br />
              // MAXIMIZING_YIELD
            </p>
          </div>

          <div className="grid grid-cols-1 lg:grid-cols-3 gap-4 lg:gap-px bg-transparent lg:bg-white/[0.08] lg:border lg:border-white/[0.08]">
            
            {/* Phase 1 */}
            <div className="group relative bg-[#030303] p-6 sm:p-10 lg:p-14 hover:bg-[#050505] transition-colors duration-500 border border-white/[0.08] lg:border-0">
              <div className="absolute top-0 right-0 p-6 opacity-0 group-hover:opacity-100 transition-opacity duration-500">
                <div className="text-[10px] font-mono text-accent">PHASE_01</div>
              </div>
              
              <div className="h-full flex flex-col justify-between">
                <div>
                  <div className="w-16 h-16 border border-white/10 bg-white/5 flex items-center justify-center mb-10 text-white group-hover:text-accent group-hover:border-accent/50 transition-all">
                    <Globe className="w-8 h-8" />
                  </div>
                  <h3 className="text-3xl text-white font-display mb-6">Capture</h3>
                  <p className="text-white/50 leading-relaxed text-lg font-light mb-12">
                    User types a generic domain (e.g. `kredit.ch`). Our DNS intercepts the request instantly. No slow redirects. No parking page ads.
                  </p>
                </div>
                <div className="pt-8 border-t border-white/[0.05]">
                   <div className="font-mono text-xs text-white/30 uppercase tracking-widest mb-2">Technical Execution</div>
                   <div className="text-white font-mono text-sm">DNS A-Record -&gt; Pounce Edge Node</div>
                </div>
              </div>
            </div>

            {/* Phase 2 */}
            <div className="group relative bg-[#030303] p-10 lg:p-14 hover:bg-[#050505] transition-colors duration-500">
              <div className="absolute top-0 right-0 p-6 opacity-0 group-hover:opacity-100 transition-opacity duration-500">
                <div className="text-[10px] font-mono text-accent">PHASE_02</div>
              </div>
              
              <div className="h-full flex flex-col justify-between">
                <div>
                  <div className="w-16 h-16 border border-white/10 bg-white/5 flex items-center justify-center mb-10 text-white group-hover:text-accent group-hover:border-accent/50 transition-all">
                    <Cpu className="w-8 h-8" />
                  </div>
                  <h3 className="text-3xl text-white font-display mb-6">Analyze</h3>
                  <p className="text-white/50 leading-relaxed text-lg font-light mb-12">
                    Our semantic engine analyzes the SLD (`kredit`) and TLD (`.ch`) to determine user intent: "Financial Service / Loan / Switzerland".
                  </p>
                </div>
                <div className="pt-8 border-t border-white/[0.05]">
                   <div className="font-mono text-xs text-white/30 uppercase tracking-widest mb-2">Processing Time</div>
                   <div className="text-white font-mono text-sm">&lt; 10ms Latency</div>
                </div>
              </div>
            </div>

            {/* Phase 3 */}
            <div className="group relative bg-[#030303] p-10 lg:p-14 hover:bg-[#050505] transition-colors duration-500">
              <div className="absolute top-0 right-0 p-6 opacity-0 group-hover:opacity-100 transition-opacity duration-500">
                <div className="text-[10px] font-mono text-accent">PHASE_03</div>
              </div>
              
              <div className="h-full flex flex-col justify-between">
                <div>
                  <div className="w-16 h-16 border border-white/10 bg-white/5 flex items-center justify-center mb-10 text-white group-hover:text-accent group-hover:border-accent/50 transition-all">
                    <Wallet className="w-8 h-8" />
                  </div>
                  <h3 className="text-3xl text-white font-display mb-6">Monetize</h3>
                  <p className="text-white/50 leading-relaxed text-lg font-light mb-12">
                    Traffic is routed to the highest-bidding partner for that vertical. You earn a commission (CPA) on conversion.
                  </p>
                </div>
                <div className="pt-8 border-t border-white/[0.05]">
                   <div className="font-mono text-xs text-white/30 uppercase tracking-widest mb-2">Outcome</div>
                   <div className="text-white font-mono text-sm">70% Revenue Share Payout</div>
                </div>
              </div>
            </div>

          </div>
        </div>
      </section>

      {/* ECOSYSTEM - Vertical & Partners */}
      <section className="relative py-32 px-4 sm:px-6 bg-[#050505] border-y border-white/[0.05]">
        <div className="max-w-[1400px] mx-auto grid lg:grid-cols-2 gap-20 items-center">
            <div>
               <span className="text-accent font-mono text-xs uppercase tracking-[0.2em] mb-4 block">The Ecosystem</span>
               <h2 className="font-display text-4xl sm:text-5xl text-white mb-8">Premium Verticals.</h2>
               <p className="text-white/50 text-lg leading-relaxed mb-10">
                  We focus on high-ticket verticals where a single lead can be worth $50-$200. No penny-clicks. No spam.
               </p>
               
               <div className="space-y-6">
                  {[
                    { name: 'Finance & Insurance', cpa: '$80 - $250', examples: 'Loans, Mortgages, Health Insurance' },
                    { name: 'Legal Services', cpa: '$100 - $400', examples: 'Lawyers, Divorce, notary' },
                    { name: 'Medical & Health', cpa: '$50 - $150', examples: 'Dentists, Cosmetic Surgery, Clinics' },
                    { name: 'B2B Services', cpa: '$40 - $120', examples: 'Marketing, SaaS, Hosting' }
                  ].map((vertical, i) => (
                    <div key={i} className="flex items-start gap-6 p-4 border border-white/5 hover:border-white/10 hover:bg-white/[0.02] transition-colors">
                        <div className="w-1.5 h-1.5 bg-accent mt-2.5 shrink-0" />
                        <div className="flex-1">
                           <div className="flex justify-between items-center mb-1">
                              <h4 className="text-white font-bold">{vertical.name}</h4>
                              <span className="text-accent font-mono text-sm">{vertical.cpa} CPA</span>
                           </div>
                           <p className="text-white/40 text-sm">{vertical.examples}</p>
                        </div>
                    </div>
                  ))}
               </div>
            </div>

            <div className="relative">
                <div className="absolute inset-0 bg-gradient-to-tr from-accent/10 to-transparent blur-3xl opacity-20" />
                <div className="bg-[#020202] border border-white/10 p-10 relative">
                    <div className="absolute top-0 left-0 p-4 border-r border-b border-white/10 bg-[#050505]">
                       <span className="text-[10px] font-mono text-white/30 uppercase tracking-widest">Partner Network</span>
                    </div>

                    <div className="mt-8 grid grid-cols-2 gap-4">
                       {[1, 2, 3, 4, 5, 6].map((_, i) => (
                          <div key={i} className="h-24 bg-white/[0.02] border border-white/5 flex flex-col items-center justify-center hover:bg-white/[0.05] transition-all group cursor-default">
                             <span className="font-mono text-sm text-white/30 uppercase tracking-widest">TBA</span>
                          </div>
                       ))}
                    </div>
                    
                    <div className="mt-8 pt-8 border-t border-white/10 text-center">
                       <p className="text-white/40 text-sm font-mono mb-4">
                          Partner announcements coming soon.
                       </p>
                       <Link
                          href="/contact"
                          className="inline-flex items-center gap-2 text-xs font-bold uppercase tracking-widest text-white hover:text-accent transition-colors"
                       >
                          Become a Partner <ArrowRight className="w-3 h-3" />
                       </Link>
                    </div>
                </div>
            </div>
        </div>
      </section>

      {/* FEATURES GRID (BENTO STYLE) */}
      <section className="relative py-40 px-4 sm:px-6 bg-[#020202]">
        <div className="max-w-[1400px] mx-auto">
            <div className="mb-20">
               <span className="text-accent font-mono text-xs uppercase tracking-[0.2em] mb-4 block">System Capabilities</span>
               <h2 className="font-display text-4xl sm:text-5xl text-white">Passive by Design.</h2>
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-px bg-white/[0.1] border border-white/[0.1]">
                {[
                    { icon: PieChart, title: 'Revenue Share', desc: 'Industry-leading 70/30 split. We only make money when you do.' },
                    { icon: Target, title: '20+ Verticals', desc: 'Finance, Insurance, Travel, Health, B2B Services, and more.' },
                    { icon: Shield, title: 'Premium Partners', desc: 'Direct API integrations with leading brands (coming soon).' },
                    { icon: BarChart3, title: 'Live Analytics', desc: 'Live dashboard showing every click, lead, and conversion.' },
                    { icon: RefreshCw, title: 'Auto-Optimization', desc: 'AI automatically routes to the best monetization path for each visitor.' },
                    { icon: Zap, title: 'Instant DNS', desc: 'Zero downtime. Verify ownership and start earning in < 5 minutes.' }
                ].map((feat, i) => (
                    <div key={i} className="bg-[#030303] p-10 hover:bg-[#080808] transition-colors group">
                        <feat.icon className="w-8 h-8 text-white/20 group-hover:text-accent transition-colors mb-6" />
                        <h3 className="text-xl font-display text-white mb-3">{feat.title}</h3>
                        <p className="text-white/40 font-mono text-sm leading-relaxed">{feat.desc}</p>
                    </div>
                ))}
            </div>
        </div>
      </section>

      {/* CTA */}
      <section className="relative py-32 px-4 sm:px-6 border-t border-white/[0.05]">
         <div className="max-w-4xl mx-auto text-center relative z-10">
            <h2 className="font-display text-5xl sm:text-6xl text-white mb-10">
               Your portfolio is <br />
               <span className="text-white/30">sleeping on gold.</span>
            </h2>
            
            {!isAuthenticated ? (
                <div className="flex flex-col items-center gap-6">
                    <Link 
                        href="/register"
                        className="px-10 py-5 bg-accent text-black text-sm font-bold uppercase tracking-[0.2em] hover:bg-white transition-all shadow-[0_0_30px_rgba(16,185,129,0.3)]"
                        style={{ clipPath: 'polygon(10px 0, 100% 0, 100% 100%, 0 100%, 0 10px)' }}
                    >
                        Start Yield Farming
                    </Link>
                    <p className="text-white/30 font-mono text-xs uppercase tracking-widest">Free for the first 30 days</p>
                </div>
            ) : (
                <Link 
                    href="/terminal/yield"
                    className="px-10 py-5 bg-white text-black text-sm font-bold uppercase tracking-[0.2em] hover:bg-accent transition-all"
                    style={{ clipPath: 'polygon(10px 0, 100% 0, 100% 100%, 0 100%, 0 10px)' }}
                >
                    Go to Dashboard
                </Link>
            )}
         </div>
      </section>

      <Footer />
    </div>
  )
}
