import { useState, useEffect } from 'react';
import { ShoppingCart, ArrowRight, Menu, X, Plus, Info, Zap, Globe, Share2, Instagram, ChevronRight, TrendingUp, Heart, ShoppingBag } from 'lucide-react';

export default function HypeTheme({ posts, categories }: { posts?: any[], categories?: any[] }) {
  const handleSimulatedAction = (e: React.MouseEvent | React.FormEvent, msg: string) => {
    e.preventDefault();
    window.alert(`[SaaS Preview] Ação simulada: ${msg}\n\nEsta funcionalidade estará ativa após a publicação do seu ecommerce.`);
  };
  const [view, setView] = useState<'home' | 'product'>('home');
  const [cartOpen, setCartOpen] = useState(false);
  const [timeLeft, setTimeLeft] = useState({ h: 23, m: 59, s: 59 });

  useEffect(() => {
    const timer = setInterval(() => {
      setTimeLeft(prev => {
        if (prev.s > 0) return { ...prev, s: prev.s - 1 };
        if (prev.m > 0) return { h: prev.h, m: prev.m - 1, s: 59 };
        if (prev.h > 0) return { h: prev.h - 1, m: 59, s: 59 };
        return prev;
      });
    }, 1000);
    return () => clearInterval(timer);
  }, []);

  useEffect(() => {
    window.scrollTo(0, 0);
  }, [view]);

  const products = [
     { id: 1, name: "OSMIUM PUFFER JACKET", price: "$380.00", image: "https://images.unsplash.com/photo-1551028719-00167b16eac5?auto=format&fit=crop&q=80&w=1200" },
     { id: 2, name: "TACTICAL CARGO PANTS", price: "$145.00", image: "https://images.unsplash.com/photo-1521572163474-6864f9cf17ab?auto=format&fit=crop&q=80&w=800" },
     { id: 3, name: "HEAVYWEIGHT HOODIE", price: "$120.00", image: "https://images.unsplash.com/photo-1556821840-3a63f95609a7?auto=format&fit=crop&q=80&w=800" },
     { id: 4, name: "COMBAT BOOTS HT-1", price: "$290.00", image: "https://images.unsplash.com/photo-1608256246200-53e635b5b65f?auto=format&fit=crop&q=80&w=800" },
  ];

  return (
    <div className="min-h-full bg-white text-black font-['Inter',sans-serif] selection:bg-[#ff0000] selection:text-white">
      
      {/* Dynamic Marquee Bar */}
      <div className="bg-[#ff0000] text-white text-[10px] font-black uppercase tracking-[0.3em] py-3 px-4 border-b-4 border-black overflow-hidden flex whitespace-nowrap">
         <div className="flex gap-20 items-center animate-[marquee_20s_linear_infinite]">
            {[1,2,3,4].map(i => (
               <div key={i} className="flex gap-20 items-center">
                  <span>⚡ NEXT DROP IN: {String(timeLeft.h).padStart(2,'0')}:{String(timeLeft.m).padStart(2,'0')}:{String(timeLeft.s).padStart(2,'0')}</span>
                  <span>NO RESTOCKS. NO REFUNDS. ALL SALES FINAL.</span>
               </div>
            ))}
         </div>
      </div>

      {/* Brutalist Navigation */}
      <nav className="border-b-4 border-black sticky top-0 z-50 bg-white">
         <div className="max-w-[1800px] mx-auto px-6 h-20 md:h-24 flex items-center justify-between">
            <div className="flex items-center gap-8">
               <button className="w-12 h-12 md:w-16 md:h-16 border-4 border-black flex items-center justify-center hover:bg-black hover:text-white transition-all shadow-[4px_4px_0_0_black] active:translate-x-1 active:translate-y-1 active:shadow-none">
                  <Menu className="w-6 h-6 md:w-8 md:h-8" />
               </button>
               <a href="/" onClick={(e) => {e.preventDefault(); setView('home');}} className="text-4xl md:text-6xl font-black italic tracking-tighter leading-none hover:skew-x-[-10deg] transition-transform uppercase">
                  HYPE<span className="text-[#ff0000]">BEAST</span>
               </a>
            </div>
            
            <div className="hidden lg:flex items-center gap-12 text-[11px] font-black uppercase tracking-[0.2em]">
               <a href="#" className="hover:text-[#ff0000] transition-colors">Drops</a>
               <a href="#" className="hover:text-[#ff0000] transition-colors">Archive</a>
               <a href="#" className="hover:text-[#ff0000] transition-colors">Manifesto</a>
            </div>

            <button onClick={() => setCartOpen(true)} className="bg-black text-white px-6 md:px-10 py-3 md:py-4 font-black uppercase tracking-widest text-[10px] md:text-xs hover:bg-[#ff0000] transition-colors flex items-center gap-3">
               CART <span className="bg-white text-black px-2 py-0.5 rounded-full text-[10px]">1</span>
            </button>
         </div>
      </nav>

      {view === 'home' ? (
        <main>
           {/* Section 1: Split Hero */}
           <section className="grid lg:grid-cols-2 border-b-4 border-black">
              <div className="p-10 md:p-24 flex flex-col justify-center bg-[#f0f0f0] border-b-4 lg:border-b-0 lg:border-r-4 border-black relative overflow-hidden group">
                 <div className="absolute top-0 right-0 p-10 opacity-5 group-hover:opacity-10 transition-opacity">
                    <TrendingUp className="w-64 h-64 text-black" />
                 </div>
                 <span className="text-[#ff0000] text-xs font-black uppercase tracking-[0.4em] mb-10 flex items-center gap-4">
                    <Zap className="w-5 h-5 animate-pulse" /> Drop_ID: 04.26_X
                 </span>
                 <h1 className="text-6xl md:text-[10rem] font-black uppercase tracking-tighter leading-[0.8] mb-12 italic">
                    {products[0].name.split(' ')[0]} <br/> <span className="text-white bg-black px-4 inline-block -rotate-2">{products[0].name.split(' ').slice(1).join(' ')}</span>
                 </h1>
                 <p className="text-lg md:text-xl font-bold uppercase leading-tight mb-16 max-w-md border-l-8 border-[#ff0000] pl-8">
                    Blindagem térmica de alta performance para o caos urbano. Resistência extrema. Edição absoluta.
                 </p>
                 <div className="flex flex-col sm:flex-row gap-6">
                    <button onClick={() => setView('product')} className="bg-[#ff0000] text-white border-4 border-black px-12 py-6 font-black uppercase tracking-widest text-sm shadow-[8px_8px_0_0_black] hover:translate-x-1 hover:translate-y-1 hover:shadow-none transition-all inline-flex items-center justify-center gap-4">
                       GET_ACCESS <ArrowRight className="w-6 h-6" />
                    </button>
                    <button className="bg-white text-black border-4 border-black px-12 py-6 font-black uppercase tracking-widest text-sm hover:bg-black hover:text-white transition-colors">
                       LOOKBOOK
                    </button>
                 </div>
              </div>
              <div className="relative aspect-square lg:aspect-auto bg-black group overflow-hidden">
                 <img src={products[0].image} className="w-full h-full object-cover grayscale opacity-70 group-hover:grayscale-0 group-hover:opacity-100 group-hover:scale-110 transition-all duration-1000" alt="hero"/>
                 <div className="absolute bottom-10 left-10 text-white font-black text-6xl italic tracking-tighter opacity-20 group-hover:opacity-50 transition-opacity">
                    CORE_COLLECTION
                 </div>
              </div>
           </section>

           {/* Section 2: Product Grid */}
           <section className="bg-white">
              <div className="p-10 border-b-4 border-black flex justify-between items-center">
                 <h2 className="text-4xl md:text-6xl font-black italic tracking-tighter uppercase">LATEST_DROPS</h2>
                 <button className="text-xs font-black uppercase tracking-widest border-b-4 border-[#ff0000] pb-1">View Archive</button>
              </div>
              <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3">
                 {products.map((p, idx) => (
                    <div key={p.id} className={`group cursor-pointer border-black flex flex-col ${idx % 3 !== 2 ? 'lg:border-r-4' : ''} border-b-4`} onClick={() => setView('product')}>
                       <div className="aspect-[4/5] bg-gray-100 relative overflow-hidden border-b-4 border-black">
                          <img src={p.image} className="w-full h-full object-cover grayscale group-hover:grayscale-0 group-hover:scale-105 transition-all duration-700" alt="product"/>
                          <div className="absolute bottom-0 left-0 right-0 bg-black text-white p-4 font-black text-[10px] uppercase tracking-widest translate-y-full group-hover:translate-y-0 transition-transform">
                             Authorized_Asset // Protocol_Verified
                          </div>
                       </div>
                       <div className="p-8 flex flex-col flex-1 bg-white group-hover:bg-black transition-colors group-hover:text-white">
                          <div className="flex justify-between items-start mb-4">
                             <span className="text-[10px] font-black uppercase tracking-widest text-[#ff0000]">Drop_0{idx+1}</span>
                             <span className="text-[10px] font-black uppercase tracking-widest opacity-40">2026_COLLECTION</span>
                          </div>
                          <h3 className="text-3xl font-black uppercase tracking-tighter mb-8 group-hover:italic transition-all">{p.name}</h3>
                          <div className="mt-auto flex justify-between items-center pt-8 border-t border-black/10 group-hover:border-white/20">
                             <span className="text-2xl font-black">{p.price}</span>
                             <ArrowRight className="w-8 h-8 opacity-0 group-hover:opacity-100 transition-opacity" />
                          </div>
                       </div>
                    </div>
                 ))}
              </div>
           </section>

           {/* Section 3: High Intensity CTA */}
           <section className="bg-[#ff0000] p-10 md:p-32 text-center text-white border-b-4 border-black relative overflow-hidden">
              <div className="absolute inset-0 opacity-10 pointer-events-none" style={{ backgroundImage: `radial-gradient(circle, black 1px, transparent 1px)`, backgroundSize: '20px 20px' }}></div>
              <h2 className="text-6xl md:text-9xl font-black italic tracking-tighter mb-12 uppercase italic relative z-10">THE_SYSTEM_IS_LIVE</h2>
              <p className="text-xl md:text-2xl font-black uppercase tracking-widest mb-16 max-w-2xl mx-auto relative z-10">Junte-se à rede de elite. Receba alertas via SMS 15 minutos antes de cada drop. Sem spam. Apenas hardware.</p>
              <div className="max-w-2xl mx-auto flex flex-col md:flex-row gap-6 relative z-10">
                 <input type="email" placeholder="ENTER_EMAIL_FOR_ACCESS" className="flex-1 bg-white text-black border-4 border-black p-6 font-black uppercase placeholder-black/30 outline-none focus:shadow-[10px_10px_0_0_black] transition-all" />
                 <button className="bg-black text-white px-12 py-6 font-black uppercase tracking-[0.2em] hover:bg-white hover:text-black transition-all shadow-[10px_10px_0_0_white]">
                    AUTHORIZE
                 </button>
              </div>
           </section>
        </main>
      ) : (
        /* --- PRODUCT DETAIL VIEW --- */
        <main>
           <div className="max-w-[1800px] mx-auto grid lg:grid-cols-2 border-b-4 border-black mb-40">
              <div className="border-b-4 lg:border-b-0 lg:border-r-4 border-black bg-[#f0f0f0] p-6 md:p-12">
                 <div className="sticky top-32">
                    <div className="aspect-[4/5] border-4 border-black bg-white relative overflow-hidden shadow-[20px_20px_0_0_black]">
                       <img src={products[0].image} className="w-full h-full object-cover" alt="product detail" />
                       <div className="absolute top-6 left-6 bg-black text-white px-4 py-2 text-xs font-black uppercase tracking-widest italic">
                          Featured_Asset
                       </div>
                    </div>
                    <div className="grid grid-cols-4 gap-4 mt-12">
                       {[1,2,3,4].map(i => (
                          <div key={i} className="aspect-square border-4 border-black bg-white hover:border-[#ff0000] cursor-pointer transition-colors p-2">
                             <img src={products[0].image} className="w-full h-full object-cover opacity-50 hover:opacity-100" />
                          </div>
                       ))}
                    </div>
                 </div>
              </div>
              
              <div className="p-10 md:p-24 flex flex-col">
                 <div className="mb-12">
                    <span className="text-[#ff0000] text-xs font-black uppercase tracking-[0.4em] mb-6 block uppercase">Asset_Code: OS_PUFF_V4</span>
                    <h1 className="text-5xl md:text-8xl font-black uppercase tracking-tighter leading-[0.8] mb-10 italic">{products[0].name}</h1>
                    <div className="text-4xl font-black bg-black text-white w-fit px-8 py-4 italic">{products[0].price}</div>
                 </div>

                 <div className="space-y-12 mb-20 border-l-8 border-[#ff0000] pl-10">
                    <h3 className="text-xs font-black uppercase tracking-[0.4em] text-black/40">DROP_MANIFESTO</h3>
                    <p className="text-2xl font-black uppercase text-black leading-tight italic">
                       Projetado para resistir ao vácuo urbano. Casca exterior em nylon ripstop 100% à prova d'água com preenchimento sintético de alta densidade. Hardware de metal pesado e branding minimalista de alto contraste.
                    </p>
                    <div className="grid grid-cols-2 gap-8 text-[10px] font-black uppercase tracking-widest text-black/30">
                       <div className="flex flex-col gap-2">
                          <span className="text-black">Material Matrix</span>
                          <span>RIPSTOP // SYNTH_DOWN</span>
                    </div>
                    <div className="flex flex-col gap-2">
                       <span className="text-black">Temp_Rating</span>
                       <span>-20°C // ACTIVE</span>
                    </div>
                 </div>
              </div>

              <div className="grid grid-cols-1 md:grid-cols-2 gap-12 mb-20">
                 <div className="space-y-6">
                    <span className="text-xs font-black uppercase tracking-widest text-black/40">Select_Size</span>
                    <div className="grid grid-cols-4 gap-4">
                       {['S', 'M', 'L', 'XL'].map(s => (
                          <button key={s} className="border-4 border-black py-4 font-black text-xl hover:bg-black hover:text-white transition-all active:bg-[#ff0000]">{s}</button>
                       ))}
                    </div>
                 </div>
              </div>

              <button onClick={() => setCartOpen(true)} className="w-full bg-[#ff0000] text-white border-4 border-black py-8 text-3xl font-black uppercase italic tracking-tighter shadow-[12px_12px_0_0_black] hover:translate-x-2 hover:translate-y-2 hover:shadow-none transition-all flex justify-center items-center gap-6">
                 ACQUIRE_ASSET <ShoppingCart className="w-10 h-10" />
              </button>
           </div>
        </div>

        {/* Section: Similar Assets */}
        {view === 'product' && (
           <div className="max-w-[1800px] mx-auto p-10 md:p-24 border-b-4 border-black">
              <h2 className="text-4xl md:text-6xl font-black italic tracking-tighter uppercase mb-20">SIMILAR_ASSETS // PROTOCOL</h2>
              <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-12">
                 {products.slice(1).map((p) => (
                    <div key={p.id} className="group cursor-pointer border-4 border-black p-6 hover:bg-black hover:text-white transition-all" onClick={() => setView('product')}>
                       <div className="aspect-[4/5] bg-gray-100 border-4 border-black relative overflow-hidden mb-8">
                          <img src={p.image} className="w-full h-full object-cover grayscale opacity-70 group-hover:grayscale-0 group-hover:opacity-100 transition-all" alt="product"/>
                       </div>
                       <h3 className="text-2xl font-black uppercase italic mb-4">{p.name}</h3>
                       <span className="text-xl font-black text-[#ff0000]">{p.price}</span>
                    </div>
                 ))}
              </div>
           </div>
        )}
        </main>
      )}

      {/* Massive Brutalist Footer */}
      <footer className="border-t-4 border-black bg-black text-white py-32 px-10">
         <div className="max-w-[1800px] mx-auto">
            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-24 mb-40">
               <div className="lg:col-span-2">
                  <h2 className="text-6xl md:text-9xl font-black italic tracking-tighter mb-12 uppercase italic">HYPE<span className="text-[#ff0000]">BEAST</span></h2>
                  <p className="text-2xl font-black uppercase tracking-tight text-white/40 leading-none max-w-xl">ESTABELECENDO O PROTOCOLO DE ESTILO URBANO DESDE 2026. SEM DESCULPAS. SEM RETROCESSOS.</p>
                  <div className="flex gap-10 mt-20">
                     <button onClick={(e) => handleSimulatedAction(e, 'Instagram')} className="w-16 h-16 border-4 border-white/20 flex items-center justify-center hover:bg-[#ff0000] hover:border-[#ff0000] transition-all"><Instagram className="w-8 h-8"/></button>
                     <button onClick={(e) => handleSimulatedAction(e, 'Site')} className="w-16 h-16 border-4 border-white/20 flex items-center justify-center hover:bg-[#ff0000] hover:border-[#ff0000] transition-all"><Globe className="w-8 h-8"/></button>
                     <button onClick={(e) => handleSimulatedAction(e, 'Share')} className="w-16 h-16 border-4 border-white/20 flex items-center justify-center hover:bg-[#ff0000] hover:border-[#ff0000] transition-all"><Share2 className="w-8 h-8"/></button>
                  </div>
               </div>
               
               <div>
                  <h4 className="text-[#ff0000] text-sm font-black uppercase tracking-[0.4em] mb-12 border-b-4 border-white/10 pb-4 inline-block">POLICIES_CORE</h4>
                  <ul className="space-y-8 text-xs font-black uppercase tracking-widest text-white/40">
                     <li><a href="#" onClick={(e) => handleSimulatedAction(e, 'Privacidade')} className="hover:text-white transition-colors flex items-center gap-4">PRIVACIDADE_DATA <ArrowRight className="w-4 h-4"/></a></li>
                     <li><a href="#" onClick={(e) => handleSimulatedAction(e, 'Termos')} className="hover:text-white transition-colors flex items-center gap-4">TERMOS_SERVICO <ArrowRight className="w-4 h-4"/></a></li>
                     <li><a href="#" onClick={(e) => handleSimulatedAction(e, 'Cookies')} className="hover:text-white transition-colors flex items-center gap-4">COOKIE_PROTOCOL <ArrowRight className="w-4 h-4"/></a></li>
                  </ul>
               </div>

               <div>
                  <h4 className="text-[#ff0000] text-sm font-black uppercase tracking-[0.4em] mb-12 border-b-4 border-white/10 pb-4 inline-block">NETWORK_ACCESS</h4>
                  <ul className="space-y-8 text-xs font-black uppercase tracking-widest text-white/40">
                     <li><a href="#" onClick={(e) => handleSimulatedAction(e, 'Suporte')} className="hover:text-white transition-colors flex items-center gap-4">TERMINAL_SUPPORT <ArrowRight className="w-4 h-4"/></a></li>
                     <li><a href="#" onClick={(e) => handleSimulatedAction(e, 'FAQ')} className="hover:text-white transition-colors flex items-center gap-4">KNOWLEDGE_BASE <ArrowRight className="w-4 h-4"/></a></li>
                     <li><a href="#" onClick={(e) => handleSimulatedAction(e, 'Envios')} className="hover:text-white transition-colors flex items-center gap-4">GLOBAL_LOGISTICS <ArrowRight className="w-4 h-4"/></a></li>
                  </ul>
               </div>
            </div>
            
            <div className="pt-20 border-t-4 border-white/5 flex flex-col md:flex-row justify-between items-center gap-10 text-[10px] font-black uppercase tracking-[0.8em] text-white/20 text-center">
               <span>© {new Date().getFullYear()} HYPEBEAST_OPERATIONS. DECODED BY AUTOBLOG.</span>
               <div className="flex items-center gap-10">
                  <span>STATUS: FULL_SYNC</span>
                  <div className="flex items-center gap-2">
                     <div className="w-2 h-2 rounded-full bg-green-500 animate-pulse"></div>
                     <span>SYSTEM_ONLINE</span>
                  </div>
               </div>
            </div>
         </div>
      </footer>

      {/* Brutalist Cart Sidebar */}
      {cartOpen && (
         <div className="fixed inset-0 z-[100] flex justify-end">
            <div className="absolute inset-0 bg-[#ff0000]/10 backdrop-blur-sm cursor-pointer" onClick={() => setCartOpen(false)}></div>
            <div className="w-full max-w-2xl bg-white border-l-8 border-black h-full flex flex-col relative z-[110] shadow-[-30px_0_0_0_#ff0000]">
               <div className="p-10 border-b-8 border-black bg-black text-white flex justify-between items-center">
                  <h2 className="text-4xl font-black italic tracking-tighter uppercase">BUFFER_01</h2>
                  <button onClick={() => setCartOpen(false)} className="hover:text-[#ff0000] transition-all"><X className="w-12 h-12"/></button>
               </div>
               
               <div className="flex-1 overflow-y-auto p-10 bg-[#f0f0f0]">
                  <div className="flex gap-8 p-6 bg-white border-8 border-black shadow-[15px_15px_0_0_#ff0000]">
                     <div className="w-32 h-44 bg-gray-100 border-4 border-black shrink-0 relative overflow-hidden">
                        <img src={products[0].image} className="w-full h-full object-cover grayscale contrast-125" alt="item"/>
                     </div>
                     <div className="flex-1 flex flex-col justify-between py-2">
                        <div>
                           <h3 className="text-2xl font-black uppercase tracking-tighter leading-none mb-4">{products[0].name}</h3>
                           <p className="text-[10px] font-black uppercase text-black/30">Protocol: OS_PUFF_V4 | Size: M</p>
                        </div>
                        <div className="flex justify-between items-end border-t-4 border-black/5 pt-6 mt-6">
                           <div className="flex items-center border-4 border-black font-black text-xl">
                              <button className="px-5 py-2 hover:bg-black hover:text-white transition-colors">-</button>
                              <span className="px-5 py-2 border-x-4 border-black">01</span>
                              <button className="px-5 py-2 hover:bg-black hover:text-white transition-colors">+</button>
                           </div>
                           <p className="text-3xl font-black italic tracking-tighter">{products[0].price}</p>
                        </div>
                     </div>
                  </div>
               </div>
               
               <div className="p-10 border-t-8 border-black bg-white">
                  <div className="flex justify-between items-center mb-10 text-4xl font-black italic tracking-tighter uppercase p-6 bg-black text-white">
                     <span>TOTAL_VAL</span>
                     <span>{products[0].price}</span>
                  </div>
                  <button className="w-full bg-[#ff0000] text-white border-8 border-black py-8 text-4xl font-black uppercase italic tracking-tighter shadow-[15px_15px_0_0_black] hover:translate-x-3 hover:translate-y-3 hover:shadow-none transition-all flex justify-center items-center gap-6">
                     CHECKOUT_SYNC <ArrowRight className="w-10 h-10" />
                  </button>
               </div>
            </div>
         </div>
      )}
    </div>
  );
}
