import Link from "next/link";
import Image from "next/image";

export default function WorkoutBuilderPage() {
  return (
    <div className="flex h-screen w-full bg-[#131313] text-[#e5e2e1] font-body selection:bg-[#c3f400] selection:text-on-primary-fixed overflow-hidden">
      {/* SideNavBar */}
      <aside className="h-screen w-72 left-0 top-0 hidden md:flex flex-col bg-[#1C1B1B] border-r border-[#353534] shrink-0">
        <div className="flex flex-col gap-8 py-8 px-4">
          <div className="px-4">
            <span className="text-[#CCFF00] font-bold text-2xl font-headline tracking-tighter italic uppercase">VivaFit 50+</span>
          </div>
          <div className="flex flex-col gap-1">
            <Link href="/dashboard" className="flex items-center gap-4 py-3 px-4 text-white opacity-60 hover:bg-[#2A2A2A] transition-colors cursor-pointer group active:scale-[0.98] duration-150 rounded-xl">
              <span className="material-symbols-outlined">dashboard</span>
              <span className="font-lexend font-semibold text-lg font-headline">Dashboard</span>
            </Link>
            <Link href="/workout/builder" className="flex items-center gap-4 py-3 text-[#CCFF00] border-l-4 border-[#CCFF00] pl-4 bg-[#2A2A2A]/30 cursor-pointer group active:scale-[0.98] duration-150 rounded-r-xl">
              <span className="material-symbols-outlined" style={{ fontVariationSettings: "'FILL' 1" }}>fitness_center</span>
              <span className="font-lexend font-semibold text-lg font-headline">Workouts</span>
            </Link>
            <Link href="/library" className="flex items-center gap-4 py-3 px-4 text-white opacity-60 hover:bg-[#2A2A2A] transition-colors cursor-pointer group active:scale-[0.98] duration-150 rounded-xl">
              <span className="material-symbols-outlined">book</span>
              <span className="font-lexend font-semibold text-lg font-headline">Library</span>
            </Link>
            <Link href="/analytics" className="flex items-center gap-4 py-3 px-4 text-white opacity-60 hover:bg-[#2A2A2A] transition-colors cursor-pointer group active:scale-[0.98] duration-150 rounded-xl">
              <span className="material-symbols-outlined">insights</span>
              <span className="font-lexend font-semibold text-lg font-headline">Analytics</span>
            </Link>
          </div>
          <div className="mt-auto px-4 pb-4">
            <div className="bg-surface-container-high rounded-xl p-4 flex flex-col gap-3">
              <div className="flex items-center gap-3">
                <div className="w-10 h-10 rounded-full bg-[#c3f400] flex items-center justify-center text-on-primary-fixed font-bold font-headline">JD</div>
                <div>
                  <p className="text-sm font-bold text-white font-headline">Sessão Ativa</p>
                  <p className="text-xs text-white/50">Montar Treino</p>
                </div>
              </div>
              <button className="w-full bg-[#CCFF00] text-[#161E00] font-bold font-headline py-3 rounded-xl hover:opacity-80 transition-opacity active:scale-95 duration-200">
                Salvar Rotina
              </button>
            </div>
          </div>
        </div>
      </aside>

      {/* Main Workspace */}
      <div className="flex-1 flex flex-col h-screen overflow-hidden">
        {/* TopAppBar */}
        <header className="flex justify-between items-center px-6 py-4 w-full bg-[#131313] z-50 border-b border-[#353534]/50">
          <div className="flex items-center gap-4">
            <span className="md:hidden material-symbols-outlined text-white">menu</span>
            <h1 className="font-lexend font-bold text-xl tracking-tight uppercase text-white font-headline">Montar Meu Treino</h1>
          </div>
          <div className="flex items-center gap-6">
            <div className="hidden md:flex gap-8 font-headline">
              <span className="text-[#CCFF00] font-bold cursor-pointer hover:opacity-80 transition-opacity">Workouts</span>
              <span className="text-white opacity-70 cursor-pointer hover:opacity-80 transition-opacity">Agenda</span>
              <span className="text-white opacity-70 cursor-pointer hover:opacity-80 transition-opacity">Comunidade</span>
            </div>
            <div className="flex gap-4">
              <span className="material-symbols-outlined text-white opacity-70 cursor-pointer hover:opacity-80 transition-opacity">history</span>
              <span className="material-symbols-outlined text-white opacity-70 cursor-pointer hover:opacity-80 transition-opacity">settings</span>
            </div>
          </div>
        </header>

        {/* Scrollable Content */}
        <main className="flex-1 overflow-y-auto p-6 md:p-10 pb-32 custom-scrollbar">
          <div className="max-w-7xl mx-auto grid grid-cols-1 lg:grid-cols-12 gap-10">
            {/* LEFT COLUMN: Selection */}
            <div className="lg:col-span-7 flex flex-col gap-12">
              {/* Step 1: Muscle Groups */}
              <section>
                <div className="flex items-center gap-3 mb-6">
                  <span className="w-8 h-8 rounded-full bg-[#c3f400] text-on-primary-fixed flex items-center justify-center font-bold font-headline">1</span>
                  <h2 className="text-2xl font-bold tracking-tight text-white uppercase italic font-headline">Grupos Musculares</h2>
                </div>
                <div className="flex flex-wrap gap-3">
                  <button className="px-6 py-3 rounded-full bg-[#c3f400] text-on-primary-fixed font-bold border-2 border-[#c3f400] transition-all hover:scale-105 active:scale-95 font-headline">Pernas</button>
                  <button className="px-6 py-3 rounded-full border-2 border-white/20 text-white font-bold hover:border-[#c3f400] transition-all active:scale-95 font-headline">Costas</button>
                  <button className="px-6 py-3 rounded-full border-2 border-white/20 text-white font-bold hover:border-[#c3f400] transition-all active:scale-95 font-headline">Braços</button>
                  <button className="px-6 py-3 rounded-full border-2 border-white/20 text-white font-bold hover:border-[#c3f400] transition-all active:scale-95 font-headline">Peito</button>
                  <button className="px-6 py-3 rounded-full border-2 border-white/20 text-white font-bold hover:border-[#c3f400] transition-all active:scale-95 font-headline">Core</button>
                  <button className="px-6 py-3 rounded-full border-2 border-white/20 text-white font-bold hover:border-[#c3f400] transition-all active:scale-95 font-headline">Corpo Inteiro</button>
                </div>
              </section>

              {/* Step 2: Exercise Selection */}
              <section>
                <div className="flex flex-col sm:flex-row items-start sm:items-center justify-between mb-6 gap-4">
                  <div className="flex items-center gap-3">
                    <span className="w-8 h-8 rounded-full bg-[#c3f400] text-on-primary-fixed flex items-center justify-center font-bold font-headline">2</span>
                    <h2 className="text-2xl font-bold tracking-tight text-white uppercase italic font-headline">Escolher Exercícios</h2>
                  </div>
                  <div className="relative group w-full sm:w-auto">
                    <span className="absolute left-3 top-1/2 -translate-y-1/2 material-symbols-outlined text-white/40">search</span>
                    <input
                      className="bg-surface-container-low border-none rounded-xl pl-10 pr-4 py-2 text-white outline-none focus:ring-2 focus:ring-[#c3f400] w-full sm:w-48 md:w-64"
                      placeholder="Buscar exercício..."
                      type="text"
                    />
                  </div>
                </div>

                {/* Bento Grid of Exercises */}
                <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                  <div className="bg-surface-container-high p-4 rounded-2xl flex items-center gap-4 group hover:bg-[#353534] transition-colors cursor-pointer border border-transparent hover:border-white/10">
                    <div className="w-20 h-20 rounded-xl bg-surface-container-lowest overflow-hidden shrink-0">
                      <img
                        className="w-full h-full object-cover opacity-80 group-hover:opacity-100 transition-opacity"
                        src="https://lh3.googleusercontent.com/aida-public/AB6AXuBpzR1x9m38LuR418LfsJ2yadc2ww5P_JIgjSkLRXtHXwtC5ZsPuoZZcXCej20fGFB-KIdAvqk7v697iSuXdfbO0qkHVtmxXXxHlyi_oz1Rt170E8CROj30EaCarKE0MDXvUMs38QxD71199-IW5GOl39-FkfBaBwom3T-5LZeQ2RaSEOk7WzXz8KMeMFGD_8BNhbzj7H7bWwAwBvMEVeEPejQkZHNZfEqEvwQpGabF6EiBdlLbm8Gz3pAJB7LuV3sESAVYHxLXEdo"
                        alt="Leg press gym machine"
                      />
                    </div>
                    <div className="flex-1">
                      <h3 className="font-bold text-white leading-tight font-headline">Leg Press 45°</h3>
                      <p className="text-sm text-white/50">Quadríceps • Médio</p>
                    </div>
                    <button className="w-10 h-10 rounded-full bg-[#c3f400]/10 text-[#c3f400] flex items-center justify-center hover:bg-[#c3f400] hover:text-on-primary-fixed transition-all">
                      <span className="material-symbols-outlined">add</span>
                    </button>
                  </div>

                  <div className="bg-surface-container-high p-4 rounded-2xl flex items-center gap-4 group hover:bg-[#353534] transition-colors cursor-pointer border border-[#c3f400]">
                    <div className="w-20 h-20 rounded-xl bg-surface-container-lowest overflow-hidden shrink-0">
                      <img
                        className="w-full h-full object-cover opacity-80 group-hover:opacity-100 transition-opacity"
                        src="https://lh3.googleusercontent.com/aida-public/AB6AXuBsY3Puv7I404eXdRRj-MpV-7j4L1-aZgGwy7rx5JYpFlV0S1L4DkiQCXkv1QpNLf1BRSsRdSNiEZmpB_AuHmwOm_Bl9dziu4ecVYEVgWW4WcjBOcawv2POwd6e0Bd4hkIXfHXM_JQhBKd4EWCPg2qKjDZCdcl2P1zFz1w4St7a8A5jlxiu3OxRcSuraXlrbyeW6iVKRtGcqiWSoYYNnt99_oGJbim8zlwztnWEvAClWHfMzaWMaHeR5RvgT_BS_zI_EWFoMYie374"
                        alt="Dumbbell set for strength training"
                      />
                    </div>
                    <div className="flex-1">
                      <h3 className="font-bold text-white leading-tight font-headline">Agachamento Livre</h3>
                      <p className="text-sm text-white/50">Pernas • Difícil</p>
                    </div>
                    <button className="w-10 h-10 rounded-full bg-[#c3f400] text-on-primary-fixed flex items-center justify-center hover:opacity-80 transition-all">
                      <span className="material-symbols-outlined" style={{ fontVariationSettings: "'FILL' 1" }}>check</span>
                    </button>
                  </div>

                  <div className="bg-surface-container-high p-4 rounded-2xl flex items-center gap-4 group hover:bg-[#353534] transition-colors cursor-pointer border border-transparent hover:border-white/10">
                    <div className="w-20 h-20 rounded-xl bg-surface-container-lowest overflow-hidden shrink-0">
                      <img
                        className="w-full h-full object-cover opacity-80 group-hover:opacity-100 transition-opacity"
                        src="https://lh3.googleusercontent.com/aida-public/AB6AXuAs2EJtEXGaW8rOGLZTBxHUajWS8-MAqJ8EZSDs9VAHMdhLHjkH6ycpGU4quVmum8Av52r1ssD_BQ_ciHhJ68ZZCo1PZYyx6xzVJQJ1ijtd7ydfiVG8kHFCK4wlwnJ2RVH0r_JDwu9DtPOrRnFiwkjuw2An7b01AfFLkfQPvPHyVYkPM6Wo5TZkGHfVa3Jzc_0zFTJAR33wWuhULdHYP7BMELfhfyKRh150GjdiqYYt3cjvPhgQBIgTCUzoJKu7W59sBTnq3hlWpO0"
                        alt="Cable machine for back row exercise"
                      />
                    </div>
                    <div className="flex-1">
                      <h3 className="font-bold text-white leading-tight font-headline">Extensão de Pernas</h3>
                      <p className="text-sm text-white/50">Quadríceps • Iniciante</p>
                    </div>
                    <button className="w-10 h-10 rounded-full bg-[#c3f400]/10 text-[#c3f400] flex items-center justify-center hover:bg-[#c3f400] hover:text-on-primary-fixed transition-all">
                      <span className="material-symbols-outlined">add</span>
                    </button>
                  </div>
                  
                  <div className="bg-surface-container-high p-4 rounded-2xl flex items-center gap-4 group hover:bg-[#353534] transition-colors cursor-pointer border border-transparent hover:border-white/10">
                    <div className="w-20 h-20 rounded-xl bg-surface-container-lowest overflow-hidden shrink-0">
                      <img
                        className="w-full h-full object-cover opacity-80 group-hover:opacity-100 transition-opacity"
                        src="https://lh3.googleusercontent.com/aida-public/AB6AXuC38XfJBhouBMHTT7JTiRMSyxvhnmyEkyc-8owv-iHGxhmg_dANEuOUFzgRavYg0XqNK719kEGt1Wm_0YhOLbSDvz5cZdQ0JwhXvIgtAXF9NZ5D4JpylZb5l6dsik3kZ0-Kt9qNx8l7m-5nbco4k_V_U-KRbnLjNKTHE8EyrJIBhzghQ8R1_GxO-7Tj8uIeq-ERFWS41H-EXzFg82dNUMO6lUwkSe3s-qOO-mK-5dT9v3EbLmsRqYRnkkfmckSWogwZKUblMzCHnvc"
                        alt="Bench press"
                      />
                    </div>
                    <div className="flex-1">
                      <h3 className="font-bold text-white leading-tight font-headline">Afundo c/ Halteres</h3>
                      <p className="text-sm text-white/50">Glúteos • Médio</p>
                    </div>
                    <button className="w-10 h-10 rounded-full bg-[#c3f400]/10 text-[#c3f400] flex items-center justify-center hover:bg-[#c3f400] hover:text-on-primary-fixed transition-all">
                      <span className="material-symbols-outlined">add</span>
                    </button>
                  </div>
                </div>
              </section>
            </div>

            {/* RIGHT COLUMN: Summary */}
            <div className="lg:col-span-5 pb-24 md:pb-0">
              <div className="sticky top-0 flex flex-col gap-6">
                <div className="flex items-center gap-3">
                  <span className="w-8 h-8 rounded-full bg-[#c3f400] text-on-primary-fixed flex items-center justify-center font-bold font-headline">3</span>
                  <h2 className="text-2xl font-bold tracking-tight text-white uppercase italic font-headline">Resumo do Treino</h2>
                </div>

                <div className="bg-surface-container-low rounded-3xl p-6 border border-white/5">
                  <div className="flex flex-col gap-4 max-h-[400px] md:max-h-[500px] overflow-y-auto pr-2 custom-scrollbar">
                    {/* Added Exercise Card */}
                    <div className="bg-surface-container-high rounded-2xl p-5 relative overflow-hidden group border border-transparent hover:border-white/10 transition-colors">
                      <div className="absolute left-0 top-0 bottom-0 w-1 bg-[#c3f400]"></div>
                      <div className="flex justify-between items-start mb-4">
                        <h4 className="font-bold text-lg text-white font-headline">Agachamento Livre</h4>
                        <span className="material-symbols-outlined text-white/30 cursor-pointer hover:text-[#ffb4ab] transition-colors">delete</span>
                      </div>
                      <div className="grid grid-cols-3 gap-3">
                        <div className="flex flex-col gap-1">
                          <label className="text-[10px] uppercase tracking-wider text-white/50 font-bold">Séries</label>
                          <input className="bg-surface-container-lowest border-none rounded-xl text-center font-bold text-[#c3f400] py-2 focus:outline-none focus:ring-1 focus:ring-[#c3f400] w-full" type="number" defaultValue="3" />
                        </div>
                        <div className="flex flex-col gap-1">
                          <label className="text-[10px] uppercase tracking-wider text-white/50 font-bold">Reps</label>
                          <input className="bg-surface-container-lowest border-none rounded-xl text-center font-bold text-[#c3f400] py-2 focus:outline-none focus:ring-1 focus:ring-[#c3f400] w-full" type="number" defaultValue="12" />
                        </div>
                        <div className="flex flex-col gap-1">
                          <label className="text-[10px] uppercase tracking-wider text-white/50 font-bold">Descanso</label>
                          <input className="bg-surface-container-lowest border-none rounded-xl text-center font-bold text-[#c3f400] py-2 focus:outline-none focus:ring-1 focus:ring-[#c3f400] w-full" type="number" defaultValue="60" />
                        </div>
                      </div>
                    </div>

                    <div className="bg-surface-container-high rounded-2xl p-5 relative overflow-hidden group border border-transparent hover:border-white/10 transition-colors">
                      <div className="absolute left-0 top-0 bottom-0 w-1 bg-white/20"></div>
                      <div className="flex justify-between items-start mb-4">
                        <h4 className="font-bold text-lg text-white font-headline">Leg Press 45°</h4>
                        <span className="material-symbols-outlined text-white/30 cursor-pointer hover:text-[#ffb4ab] transition-colors">delete</span>
                      </div>
                      <div className="grid grid-cols-3 gap-3">
                        <div className="flex flex-col gap-1">
                          <label className="text-[10px] uppercase tracking-wider text-white/50 font-bold">Séries</label>
                          <input className="bg-surface-container-lowest border-none rounded-xl text-center font-bold text-[#c3f400] py-2 focus:outline-none focus:ring-1 focus:ring-[#c3f400] w-full" type="number" defaultValue="4" />
                        </div>
                        <div className="flex flex-col gap-1">
                          <label className="text-[10px] uppercase tracking-wider text-white/50 font-bold">Reps</label>
                          <input className="bg-surface-container-lowest border-none rounded-xl text-center font-bold text-[#c3f400] py-2 focus:outline-none focus:ring-1 focus:ring-[#c3f400] w-full" type="number" defaultValue="10" />
                        </div>
                        <div className="flex flex-col gap-1">
                          <label className="text-[10px] uppercase tracking-wider text-white/50 font-bold">Descanso</label>
                          <input className="bg-surface-container-lowest border-none rounded-xl text-center font-bold text-[#c3f400] py-2 focus:outline-none focus:ring-1 focus:ring-[#c3f400] w-full" type="number" defaultValue="90" />
                        </div>
                      </div>
                    </div>
                  </div>

                  <div className="mt-8 pt-6 border-t border-white/5">
                    <div className="flex justify-between items-end mb-6">
                      <div>
                        <p className="text-white/50 text-xs uppercase tracking-widest font-bold font-headline">Tempo Estimado</p>
                        <p className="text-3xl font-black text-white italic tracking-tighter">45<span className="text-[#c3f400]">MIN</span></p>
                      </div>
                      <div className="text-right">
                        <p className="text-white/50 text-xs uppercase tracking-widest font-bold font-headline">Volume Total</p>
                        <p className="text-xl font-bold text-white">7 Exercícios</p>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </main>

        {/* Bottom Global Actions (Fixed) */}
        <div className="fixed bottom-[72px] md:bottom-0 left-0 md:left-72 right-0 bg-[#353534]/80 backdrop-blur-xl border-t border-white/5 z-[60] p-4 md:p-6">
          <div className="max-w-7xl mx-auto flex flex-col sm:flex-row gap-4">
            <button className="flex-1 h-14 md:h-16 rounded-2xl border-2 border-white/10 text-white font-black text-lg uppercase tracking-wider hover:bg-white/5 transition-all active:scale-95 duration-200 font-headline">
              Salvar Treino
            </button>
            <Link href="/workout/active" className="flex-2 h-14 md:h-16 rounded-2xl bg-[#C3F400] text-[#161E00] font-black text-lg md:text-xl uppercase tracking-widest shadow-[0_0_40px_rgba(195,244,0,0.2)] hover:shadow-[0_0_60px_rgba(195,244,0,0.3)] transition-all active:scale-95 duration-200 flex items-center justify-center gap-3">
              <span className="material-symbols-outlined font-bold">play_arrow</span>
              Começar Agora
            </Link>
          </div>
        </div>

        {/* BottomNavBar (Mobile Only) */}
        <nav className="fixed bottom-0 left-0 w-full flex justify-around items-center px-4 pb-6 pt-3 bg-[#1C1B1B] md:hidden z-[70] shadow-[0_-10px_30px_rgba(195,244,0,0.05)] border-t border-white/5">
          <Link href="/dashboard" className="flex flex-col items-center justify-center text-white opacity-50 px-4 py-2 hover:opacity-100 scale-90 duration-200">
            <span className="material-symbols-outlined">home</span>
            <span className="font-lexend font-semibold text-[12px] uppercase tracking-wider">Home</span>
          </Link>
          <Link href="/workout/builder" className="flex flex-col items-center justify-center bg-[#C3F400] text-[#161E00] rounded-xl px-4 py-2 scale-90 duration-200">
            <span className="material-symbols-outlined" style={{ fontVariationSettings: "'FILL' 1" }}>add_circle</span>
            <span className="font-lexend font-semibold text-[12px] uppercase tracking-wider">Build</span>
          </Link>
          <Link href="/library" className="flex flex-col items-center justify-center text-white opacity-50 px-4 py-2 hover:opacity-100 scale-90 duration-200">
            <span className="material-symbols-outlined">format_list_bulleted</span>
            <span className="font-lexend font-semibold text-[12px] uppercase tracking-wider">Exercises</span>
          </Link>
          <Link href="/profile" className="flex flex-col items-center justify-center text-white opacity-50 px-4 py-2 hover:opacity-100 scale-90 duration-200">
            <span className="material-symbols-outlined">person</span>
            <span className="font-lexend font-semibold text-[12px] uppercase tracking-wider">Profile</span>
          </Link>
        </nav>
      </div>

      <style dangerouslySetInnerHTML={{__html: `
        .custom-scrollbar::-webkit-scrollbar { width: 6px; }
        .custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
        .custom-scrollbar::-webkit-scrollbar-thumb { background: #353534; border-radius: 10px; }
      `}} />
    </div>
  );
}
