import Link from "next/link";
import Image from "next/image";
import { getCoupleStatus } from '@/actions/couple'
import CoupleManager from "./CoupleManager";

export default async function CoupleModePage() {
  const coupleStatus = await getCoupleStatus()
  
  return (
    <div className="bg-[#131313] text-[#e5e2e1] font-body selection:bg-[#c3f400] selection:text-on-primary-fixed">
      <div className="flex h-screen overflow-hidden">
        {/* Side Navigation (Shared Component) */}
        <aside className="hidden md:flex flex-col h-full w-72 bg-surface-container-low p-4 gap-2 z-50 border-r border-[#353534] shrink-0">
          <div className="mb-8 px-4 py-4">
            <h1 className="text-2xl font-black text-[#c3f400] tracking-tight italic uppercase font-headline">VivaFit 50+</h1>
            <p className="text-xs text-white/50 mt-1">Bem-vindo de volta</p>
          </div>
          <nav className="flex flex-col gap-1">
            <Link href="/dashboard" className="flex items-center gap-4 text-white px-4 py-3 rounded-xl hover:bg-[#2a2a2a] transition-all group duration-200 opacity-70 hover:opacity-100">
              <span className="material-symbols-outlined group-hover:text-[#c3f400] transition-colors">dashboard</span>
              <span className="font-medium text-base font-headline">Dashboard</span>
            </Link>
            <Link href="/workout/builder" className="flex items-center gap-4 text-white px-4 py-3 rounded-xl hover:bg-[#2a2a2a] transition-all group duration-200 opacity-70 hover:opacity-100">
              <span className="material-symbols-outlined group-hover:text-[#c3f400] transition-colors">fitness_center</span>
              <span className="font-medium text-base font-headline">Treinos</span>
            </Link>
            {/* Active Tab: Modo Casal */}
            <Link href="/couple" className="flex items-center gap-4 bg-[#c3f400] text-on-primary-fixed rounded-xl font-bold px-4 py-3 transition-all">
              <span className="material-symbols-outlined" style={{ fontVariationSettings: "'FILL' 1" }}>favorite</span>
              <span className="font-medium text-base font-headline">Modo Casal</span>
            </Link>
            <Link href="/library" className="flex items-center gap-4 text-white px-4 py-3 rounded-xl hover:bg-[#2a2a2a] transition-all group duration-200 opacity-70 hover:opacity-100">
              <span className="material-symbols-outlined group-hover:text-[#c3f400] transition-colors">menu_book</span>
              <span className="font-medium text-base font-headline">Biblioteca</span>
            </Link>
            <Link href="/profile" className="flex items-center gap-4 text-white px-4 py-3 rounded-xl hover:bg-[#2a2a2a] transition-all group duration-200 opacity-70 hover:opacity-100">
              <span className="material-symbols-outlined group-hover:text-[#c3f400] transition-colors">person</span>
              <span className="font-medium text-base font-headline">Perfil</span>
            </Link>
          </nav>
          
          <div className="mt-auto p-4 bg-surface-container-high rounded-2xl border border-white/5">
            <div className="flex items-center gap-3 mb-4">
              <div className="w-10 h-10 rounded-full bg-[#c3f400]/20 flex items-center justify-center">
                <span className="material-symbols-outlined text-[#c3f400]">bolt</span>
              </div>
              <div>
                <p className="text-xs text-white/50 uppercase font-bold tracking-widest font-headline">Premium</p>
                <p className="text-sm font-semibold text-white">Plano Ativo</p>
              </div>
            </div>
            <Link href="/workout/active" className="w-full flex items-center justify-center py-3 bg-[#c3f400] text-on-primary-fixed rounded-xl font-bold text-sm hover:scale-[0.98] active:scale-95 transition-transform font-headline">
              Iniciar Treino
            </Link>
          </div>
        </aside>

        {/* Main Content Canvas */}
        <main className="flex-1 flex flex-col overflow-y-auto custom-scrollbar bg-[#131313] relative">
          {/* TopAppBar (Shared Component) */}
          <header className="flex justify-between items-center px-6 py-4 w-full sticky top-0 bg-[#131313]/90 backdrop-blur-md z-40 border-b border-[#353534]/50">
            <div>
              <h2 className="font-lexend font-semibold text-lg md:hidden text-white font-headline">Modo Casal</h2>
            </div>
            <div className="flex items-center gap-4 ml-auto">
              <button className="p-2 bg-surface-container-low rounded-full hover:bg-[#2a2a2a] transition-colors relative text-white border border-white/5">
                <span className="material-symbols-outlined">notifications</span>
                <span className="absolute top-2 right-2 w-2 h-2 bg-[#ffb4a4] rounded-full"></span>
              </button>
              <button className="p-2 bg-surface-container-low rounded-full hover:bg-[#2a2a2a] transition-colors text-white border border-white/5">
                <span className="material-symbols-outlined">search</span>
              </button>
              <Link href="/profile" className="w-10 h-10 rounded-full overflow-hidden border-2 border-[#c3f400] block">
                <img alt="User profile photo" className="w-full h-full object-cover" src="https://lh3.googleusercontent.com/aida-public/AB6AXuANDdZhJtMzuf6rF2DuIHhk3kXd9x3qh4zmgtXmBQu8z205wObFYR9G-pEB3OITwpSI-BGD1iphHw3czXMt3jQkRca1D7rjmLm5qKIkKw0sgTXJKRafG-TqQ4aSoZB0WzjHUDkCt8yyfOXfa0RIpWf9it6mYgQReFa5Y4CTIDzQpgce1JL4f3Cj2CjJyf1RAntQqS7NFjc1e8tSQyFd9xns-aWR4sG3KOPE1TAR_mFMJ65GAgsH538L3rM8IEP8q_UHwJlHhZhHkSw" />
              </Link>
            </div>
          </header>

          <div className="p-4 sm:p-6 max-w-7xl mx-auto w-full space-y-8 pb-32 md:pb-12">
            {/* Hero Section: Header & Dual Profile Cards */}
            <section className="space-y-6">
              <div className="flex flex-col md:flex-row md:items-end justify-between gap-4">
                <div>
                  <span className="text-[#c3f400] font-bold tracking-widest text-xs uppercase font-headline">Conexão Vital</span>
                  <h3 className="text-4xl md:text-5xl font-black text-white mt-1 tracking-tight font-headline italic">Modo Casal</h3>
                </div>
                <div className="flex items-center gap-2 bg-surface-container-high px-4 py-2 rounded-full border border-white/10 self-start md:self-auto">
                  <span className="material-symbols-outlined text-[#ffb4a4]" style={{ fontVariationSettings: "'FILL' 1" }}>favorite</span>
                  <span className="text-sm font-bold text-white">12 dias seguidos</span>
                </div>
              </div>

              {/* Bento Grid for Profile Cards */}
              <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
                {/* Profile Card: Maria Silva */}
                <div className="bg-surface-container-low rounded-4xl p-6 sm:p-8 relative overflow-hidden group border border-white/5 hover:border-[#c3f400]/30 transition-colors">
                  <div className="absolute -right-12 -top-12 w-48 h-48 bg-[#c3f400]/5 rounded-full blur-3xl group-hover:bg-[#c3f400]/10 transition-all"></div>
                  <div className="flex flex-col sm:flex-row justify-between items-start gap-4 relative z-10">
                    <div className="flex gap-4 items-center">
                      <div className="w-16 h-16 sm:w-20 sm:h-20 rounded-3xl overflow-hidden border-4 border-[#2a2a2a]">
                        <img alt="Maria Silva Profile" className="w-full h-full object-cover" src="https://lh3.googleusercontent.com/aida-public/AB6AXuC167bpHlcSupum8nA63UeGcjfyMlFoXKHivvT4_CcPvnVPbryeXUfYcoRaUo7sRxEkPCiGQlVWr4GyJ-w5va9-6dVlSZkm4T33ie35k7-5btaWzgyJsR5EZ3nHgYyHaM9Kvb1mnK9VwlmsHbT46QGYXeSaNFUAKh3oRwYiN1tXaP5pAUrXj4rt4LPzJoJHgsCsceUDNpHCT22OI2joyWZ8PuYHIHzUF0pPIn-7KK6rLB089fEBIREgo22kgVw4d7CJEPakoGfGkUE" />
                      </div>
                      <div>
                        <h4 className="text-xl sm:text-2xl font-bold text-white font-headline">Maria Silva</h4>
                        <p className="text-white/70 font-medium text-sm sm:text-base">Meta Diária: 85%</p>
                      </div>
                    </div>
                    <div className="sm:text-right w-full sm:w-auto flex justify-between sm:block">
                      <span className="block text-3xl sm:text-4xl font-black text-[#c3f400] font-headline">7.4k</span>
                      <span className="text-xs uppercase tracking-widest text-white/50 font-bold font-headline self-end sm:self-auto">Passos</span>
                    </div>
                  </div>
                  
                  <div className="mt-8 space-y-4 relative z-10">
                    <div className="w-full h-3 bg-[#353534] rounded-full overflow-hidden">
                      <div className="h-full bg-[#c3f400] rounded-full w-[85%] shadow-[0_0_10px_rgba(195,244,0,0.3)]"></div>
                    </div>
                    <div className="flex justify-between items-center text-xs sm:text-sm font-semibold">
                      <span className="flex items-center gap-2 text-white">
                        <span className="material-symbols-outlined text-[#c3f400] text-lg">history</span>
                        Yoga Matinal (30min)
                      </span>
                      <span className="text-white/50">Há 1h</span>
                    </div>
                  </div>
                </div>

                {/* Profile Card: João Silva */}
                <div className="bg-surface-container-low rounded-4xl p-6 sm:p-8 relative overflow-hidden group border border-white/5 hover:border-[#ffb4a4]/30 transition-colors">
                  <div className="absolute -right-12 -top-12 w-48 h-48 bg-[#ffb4a4]/5 rounded-full blur-3xl group-hover:bg-[#ffb4a4]/10 transition-all"></div>
                  <div className="flex flex-col sm:flex-row justify-between items-start gap-4 relative z-10">
                    <div className="flex gap-4 items-center">
                      <div className="w-16 h-16 sm:w-20 sm:h-20 rounded-3xl overflow-hidden border-4 border-[#2a2a2a]">
                        <img alt="João Silva Profile" className="w-full h-full object-cover" src="https://lh3.googleusercontent.com/aida-public/AB6AXuCqqq-UQ9CdB2pSpVvlxjUh2UYmzLlWRyEVkD7MQIGQU_CzolPbgcbsGZph77wPXKTElNedcRiJXeA6NJEGHTgYTBuoFL3aLX7BxQQ2-UbAw-eGmredj_xl_ey3gS-LP77e25Lib9IfNj70JULotMH5Sqq0I8ySr4FuiRmZCosHNIURmJwOkN1Shn-yqyvbcPoxJVXd3_GzQcimGGycv5bpxvRdM1Tdhp9Y9YL4BoCwMDmcHha9JlPQOQPnaynmmPfvKqxLiMgMowM" />
                      </div>
                      <div>
                        <h4 className="text-xl sm:text-2xl font-bold text-white font-headline">João Silva</h4>
                        <p className="text-white/70 font-medium text-sm sm:text-base">Meta Diária: 62%</p>
                      </div>
                    </div>
                    <div className="sm:text-right w-full sm:w-auto flex justify-between sm:block">
                      <span className="block text-3xl sm:text-4xl font-black text-[#ffb4a4] font-headline">5.1k</span>
                      <span className="text-xs uppercase tracking-widest text-white/50 font-bold font-headline self-end sm:self-auto">Passos</span>
                    </div>
                  </div>
                  
                  <div className="mt-8 space-y-4 relative z-10">
                    <div className="w-full h-3 bg-[#353534] rounded-full overflow-hidden">
                      <div className="h-full bg-[#ffb4a4] rounded-full w-[62%] shadow-[0_0_10px_rgba(255,180,164,0.3)]"></div>
                    </div>
                    <div className="flex justify-between items-center text-xs sm:text-sm font-semibold">
                      <span className="flex items-center gap-2 text-white">
                        <span className="material-symbols-outlined text-[#ffb4a4] text-lg">history</span>
                        Caminhada Rápida (15min)
                      </span>
                      <span className="text-white/50">Há 3h</span>
                    </div>
                  </div>
                </div>
              </div>
            </section>

            {/* Shared Goals & Progress Chart */}
            <section className="grid grid-cols-1 lg:grid-cols-3 gap-8">
              {/* Shared Goals (2/3 width on large screens) */}
              <div className="lg:col-span-2 space-y-6">
                <div className="flex items-center justify-between">
                  <h4 className="text-2xl font-bold text-white font-headline italic">Metas Compartilhadas</h4>
                  <button className="text-[#c3f400] font-bold text-xs sm:text-sm uppercase tracking-wider font-headline hover:text-white transition-colors">Ver Tudo</button>
                </div>
                
                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                  <div className="bg-surface-container-low p-6 rounded-3xl border border-white/5 hover:border-white/10 transition-colors">
                    <div className="flex items-center justify-between mb-4">
                      <div className="p-3 bg-[#c3f400]/10 rounded-2xl">
                        <span className="material-symbols-outlined text-[#c3f400] text-2xl" style={{ fontVariationSettings: "'FILL' 1" }}>local_fire_department</span>
                      </div>
                      <span className="text-2xl font-black text-white font-headline">1.2k / 1.5k</span>
                    </div>
                    <p className="font-bold text-lg mb-1 text-white font-headline">Calorias Juntos</p>
                    <p className="text-sm text-white/50 mb-4">Faltam 300 kcal para o recorde semanal</p>
                    <div className="w-full h-2 bg-[#353534] rounded-full overflow-hidden">
                      <div className="h-full bg-[#c3f400] w-[80%] shadow-[0_0_10px_rgba(195,244,0,0.3)]"></div>
                    </div>
                  </div>
                  
                  <div className="bg-surface-container-low p-6 rounded-3xl border border-white/5 hover:border-white/10 transition-colors">
                    <div className="flex items-center justify-between mb-4">
                      <div className="p-3 bg-[#ffb4a4]/10 rounded-2xl">
                        <span className="material-symbols-outlined text-[#ffb4a4] text-2xl" style={{ fontVariationSettings: "'FILL' 1" }}>calendar_month</span>
                      </div>
                      <span className="text-2xl font-black text-white font-headline">3 / 5</span>
                    </div>
                    <p className="font-bold text-lg mb-1 text-white font-headline">Treinos na Semana</p>
                    <p className="text-sm text-white/50 mb-4">Mantenham o ritmo para ganhar o selo</p>
                    <div className="w-full h-2 bg-[#353534] rounded-full overflow-hidden">
                      <div className="h-full bg-[#ffb4a4] w-[60%] shadow-[0_0_10px_rgba(255,180,164,0.3)]"></div>
                    </div>
                  </div>
                </div>
              </div>

              {/* Growth Chart Mockup */}
              <div className="bg-surface-container-low p-6 sm:p-8 rounded-4xl border border-white/5">
                <h5 className="text-xl font-bold mb-6 text-white font-headline italic">Evolução em Dupla <span className="text-white/50 text-sm font-normal ml-1 sm:ml-2 not-italic">(Min. Ativos)</span></h5>
                
                <div className="flex items-end justify-between h-40 sm:h-48 gap-1 sm:gap-2">
                  <div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 group">
                    <div className="w-full flex flex-col justify-end h-full">
                      <div className="bg-[#ffb4a4]/40 w-full rounded-t-lg h-[40%] group-hover:bg-[#ffb4a4]/60 transition-colors"></div>
                      <div className="bg-[#c3f400]/40 w-full rounded-b-lg h-[30%] group-hover:bg-[#c3f400]/60 transition-colors"></div>
                    </div>
                    <span className="text-[10px] sm:text-xs font-bold text-white/50 uppercase">Seg</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 group">
                    <div className="w-full flex flex-col justify-end h-full">
                      <div className="bg-[#ffb4a4]/60 w-full rounded-t-lg h-[50%] group-hover:bg-[#ffb4a4]/80 transition-colors"></div>
                      <div className="bg-[#c3f400]/60 w-full rounded-b-lg h-[45%] group-hover:bg-[#c3f400]/80 transition-colors"></div>
                    </div>
                    <span className="text-[10px] sm:text-xs font-bold text-white/50 uppercase">Ter</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 group">
                    <div className="w-full flex flex-col justify-end h-full">
                      <div className="bg-[#ffb4a4] w-full rounded-t-lg h-[30%] opacity-80 group-hover:opacity-100 transition-colors"></div>
                      <div className="bg-[#c3f400] w-full rounded-b-lg h-[20%] opacity-80 group-hover:opacity-100 transition-colors"></div>
                    </div>
                    <span className="text-[10px] sm:text-xs font-bold text-white/50 uppercase">Qua</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 group">
                    <div className="w-full flex flex-col justify-end h-full">
                      <div className="bg-[#ffb4a4]/80 w-full rounded-t-lg h-[65%] group-hover:bg-[#ffb4a4] transition-colors"></div>
                      <div className="bg-[#c3f400]/80 w-full rounded-b-lg h-[55%] group-hover:bg-[#c3f400] transition-colors"></div>
                    </div>
                    <span className="text-[10px] sm:text-xs font-bold text-white/50 uppercase">Qui</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 group">
                    <div className="w-full flex flex-col justify-end h-full relative">
                      <div className="absolute -top-8 left-1/2 -translate-x-1/2 bg-[#353534] py-1 px-2 rounded text-[10px] font-bold text-white opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap z-10">Total: 95m</div>
                      <div className="bg-[#ffb4a4] w-full rounded-t-lg h-[80%] shadow-[0_0_10px_rgba(255,180,164,0.2)]"></div>
                      <div className="bg-[#c3f400] w-full rounded-b-lg h-[75%] shadow-[0_0_10px_rgba(195,244,0,0.2)]"></div>
                    </div>
                    <span className="text-[10px] sm:text-xs font-bold text-[#c3f400] uppercase font-headline">Hoje</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 opacity-30">
                    <div className="w-full flex flex-col justify-end h-full">
                      <div className="w-full bg-[#353534] rounded-lg h-12"></div>
                    </div>
                    <span className="text-[10px] sm:text-xs font-bold text-white/50 uppercase">Sab</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center gap-2 sm:gap-3 opacity-30">
                    <div className="w-full flex flex-col justify-end h-full">
                      <div className="w-full bg-[#353534] rounded-lg h-8"></div>
                    </div>
                    <span className="text-[10px] sm:text-xs font-bold text-white/50 uppercase">Dom</span>
                  </div>
                </div>
              </div>
            </section>

            {/* Manage Connection & CTA & Motivation */}
            <div className="grid grid-cols-1 lg:grid-cols-3 gap-8 pb-8">
              
              <div className="lg:col-span-2 bg-surface-container-low p-6 sm:p-8 rounded-4xl border border-white/5">
                <h4 className="text-xl font-bold mb-6 text-white font-headline italic">Gerenciar Parceiro</h4>
                
                <CoupleManager initialStatus={coupleStatus} />
                
                <div className="mt-8 pt-8 border-t border-white/5">
                  <Link href="/workout/active" className="w-full flex items-center justify-center bg-[#c3f400] text-on-primary-fixed py-4 sm:py-5 rounded-2xl font-black text-lg shadow-[0_0_30px_rgba(195,244,0,0.15)] hover:shadow-[0_0_40px_rgba(195,244,0,0.25)] active:scale-95 transition-all font-headline uppercase tracking-wider">
                    Iniciar Treino em Dupla
                  </Link>
                  <p className="text-center text-xs text-white/50 mt-4 font-medium italic">&quot;O melhor incentivo é estar junto.&quot;</p>
                </div>
              </div>

              {/* Motivational Badge Section */}
              <div className="bg-[#b72301] p-6 sm:p-8 rounded-4xl text-white relative overflow-hidden flex flex-col justify-center">
                <div className="relative z-10">
                  <h5 className="font-black text-2xl sm:text-3xl mb-3 font-headline italic">Novo Desafio!</h5>
                  <p className="text-sm sm:text-base font-medium opacity-90 leading-relaxed">Concluam 3 treinos juntos esta semana para desbloquear o selo &apos;Duo Dinâmico&apos;.</p>
                </div>
                <span className="material-symbols-outlined absolute -right-4 -bottom-4 text-8xl sm:text-9xl opacity-20 rotate-12" style={{ fontVariationSettings: "'FILL' 1" }}>military_tech</span>
                <div className="absolute top-0 right-0 w-32 h-32 bg-white/10 blur-2xl rounded-full mix-blend-overlay"></div>
              </div>
              
            </div>
          </div>
        </main>

        {/* Bottom Navigation (Shared Component - Mobile Only) */}
        <nav className="md:hidden fixed bottom-0 left-0 w-full bg-surface-container-low flex justify-around items-center px-4 pb-6 pt-3 z-50 border-t border-white/5 shadow-[0_-4px_20px_rgba(195,244,0,0.05)]">
          <Link href="/dashboard" className="flex flex-col items-center gap-1 text-white/50 hover:text-white transition-colors group">
            <span className="material-symbols-outlined group-hover:scale-110 transition-transform">dashboard</span>
            <span className="text-[10px] font-bold uppercase tracking-wider font-headline">Home</span>
          </Link>
          <Link href="/workout/builder" className="flex flex-col items-center gap-1 text-white/50 hover:text-white transition-colors group">
            <span className="material-symbols-outlined group-hover:scale-110 transition-transform">fitness_center</span>
            <span className="text-[10px] font-bold uppercase tracking-wider font-headline">Treinos</span>
          </Link>
          <Link href="/couple" className="flex flex-col items-center justify-center bg-[#c3f400] text-on-primary-fixed rounded-xl px-6 py-2 active:scale-95 transition-transform shrink-0 shadow-[0_0_15px_rgba(195,244,0,0.1)] -mt-4 border-4 border-[#131313]">
            <span className="material-symbols-outlined mb-1 text-xl" style={{ fontVariationSettings: "'FILL' 1" }}>favorite</span>
            <span className="text-[10px] font-bold uppercase tracking-wider font-headline">Casal</span>
          </Link>
          <Link href="/library" className="flex flex-col items-center gap-1 text-white/50 hover:text-white transition-colors group">
            <span className="material-symbols-outlined group-hover:scale-110 transition-transform">menu_book</span>
            <span className="text-[10px] font-bold uppercase tracking-wider font-headline">Livraria</span>
          </Link>
          <Link href="/profile" className="flex flex-col items-center gap-1 text-white/50 hover:text-white transition-colors group">
            <span className="material-symbols-outlined group-hover:scale-110 transition-transform">person</span>
            <span className="text-[10px] font-bold uppercase tracking-wider font-headline">Perfil</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>
  );
}
