"use client";

import Link from "next/link";
import { useEffect, useState } from "react";
import { createClient } from "@/utils/supabase/client";
import { WorkoutCard } from "@/components/WorkoutCard";

export default function DashboardPage() {
  const [treinos, setTreinos] = useState<any[]>([]);
  const [loading, setLoading] = useState(true);
  const supabase = createClient();

  useEffect(() => {
    async function loadTreinos() {
      const { data, error } = await supabase
        .from('treinos')
        .select('*');
      
      if (data) {
        setTreinos(data);
      }
      setLoading(false);
    }
    loadTreinos();
  }, []);

  return (
    <div className="flex min-h-[calc(100vh-72px)] bg-[#121212]">
      {/* SideNavBar Anchor */}
      <aside className="hidden md:flex flex-col h-screen w-64 bg-[#0A0A0A] fixed left-0 top-0 bottom-0 p-4 gap-2 z-50 border-r border-[#424242]">
        <div className="mb-10 px-4 mt-4">
          <h1 className="text-xl font-black neon-text tracking-tight uppercase font-headline">VivaFit 50+</h1>
          <p className="text-on-surface-variant text-base mt-1">Bem-vindo de volta</p>
        </div>
        <nav className="flex flex-col gap-2 grow">
          <Link href="/dashboard" className="bg-neon text-[#161E00] rounded-xl font-bold px-4 py-3 flex items-center gap-3 transition-all">
            <span className="material-symbols-outlined" style={{ fontVariationSettings: "'FILL' 1" }}>dashboard</span>
            <span>Dashboard</span>
          </Link>
          <Link href="/workout/builder" className="text-[#FFFFFF] px-4 py-3 flex items-center gap-3 hover:bg-surface-variant rounded-xl transition-all">
            <span className="material-symbols-outlined">fitness_center</span>
            <span>Treinos</span>
          </Link>
          <Link href="/library" className="text-[#FFFFFF] px-4 py-3 flex items-center gap-3 hover:bg-surface-variant rounded-xl transition-all">
            <span className="material-symbols-outlined">menu_book</span>
            <span>Biblioteca</span>
          </Link>
          <Link href="/profile" className="text-[#FFFFFF] px-4 py-3 flex items-center gap-3 hover:bg-surface-variant rounded-xl transition-all">
            <span className="material-symbols-outlined">person</span>
            <span>Perfil</span>
          </Link>
        </nav>
        <div className="mt-auto p-4 flex items-center gap-4 bg-surface-container-high rounded-2xl border border-[#424242]">
          <img
            alt="User avatar"
            className="w-12 h-12 rounded-full object-cover border-2 border-[#CCFF00]"
            src="https://lh3.googleusercontent.com/aida-public/AB6AXuA_dpV3X_kRmt_6aDpIKe7YpwWrIFFVcZr-N4vYVGtTg5vsKKVQS4qF8jcuBH9EjhYjiOlASKE9I0AsNU6FrXqw7hOqrYZsUWotwWUrVMEAsRuBoEXZu88JiauKAwZwgNsZYMwli5B0SakDDL7XptZp63H2riNnJcREyTWaGKjSuV6chLp4BVmwQsSxVT4JypGC6h8nftEDuFR-QdRYOvWX5jkZq1y0YGXMZ6mB2CaHlLH_cZ0bTa0SZnIOcI2XqtRKzLTlLUEjJfk"
          />
          <div className="flex flex-col overflow-hidden">
            <span className="font-semibold text-base truncate">Maria Silva</span>
            <span className="text-base text-on-surface-variant">Nível Intermediário</span>
          </div>
        </div>
      </aside>

      {/* Main Content Canvas */}
      <main className="flex-1 md:ml-64 p-4 sm:p-6 md:p-10 max-w-4xl mx-auto w-full mb-28 md:mb-0">
        {/* TopAppBar Context */}
        <header className="flex justify-between items-center mb-8 w-full mt-2 md:mt-0">
          <div className="md:hidden">
            <h1 className="text-2xl font-bold text-[#FFFFFF] tracking-tight uppercase font-headline">VivaFit 50+</h1>
          </div>
          <div className="hidden md:block">
            <h2 className="text-4xl font-black text-white tracking-tight font-headline">Painel Principal</h2>
          </div>
          <div className="flex items-center gap-4">
            <button className="flex items-center justify-center w-12 h-12 text-white hover:bg-surface-variant rounded-full transition-colors">
              <span className="material-symbols-outlined text-2xl">search</span>
            </button>
            <button className="flex items-center justify-center w-12 h-12 text-white hover:bg-surface-variant rounded-full transition-colors relative">
              <span className="material-symbols-outlined text-2xl">notifications</span>
              <span className="absolute top-2 right-2 w-3 h-3 bg-[#CCFF00] rounded-full border-2 border-[#121212]"></span>
            </button>
          </div>
        </header>

        {/* Responsive Grid Layout */}
        <div className="w-full flex flex-col gap-10">
          
          {/* Section 1: Hero Training Card (Supabase Integrated for 50+) */}
          <section className="w-full">
            <h3 className="text-2xl font-black text-white tracking-tight mb-6 font-headline uppercase">Seu Treino de Hoje</h3>
            
            {loading ? (
              <div className="w-full bg-[#1F1F1F] rounded-3xl p-6 border-2 border-[#121212] animate-pulse min-h-[250px] flex flex-col justify-center items-center shadow-xl">
                <span className="material-symbols-outlined text-[#CCFF00] text-5xl mb-4 animate-spin">sync</span>
                <p className="text-white text-lg font-bold">Carregando seus treinos...</p>
              </div>
            ) : treinos.length === 0 ? (
              <div className="w-full bg-[#121212] rounded-3xl p-8 border-2 border-dashed border-[#CCFF00]/50 flex flex-col items-center justify-center text-center gap-6 shadow-xl py-12">
                <div className="bg-[#CCFF00]/10 p-5 rounded-full">
                  <span className="material-symbols-outlined text-[#CCFF00] text-6xl">fitness_center</span>
                </div>
                <div>
                  <h4 className="text-2xl md:text-3xl font-black text-white mb-3">Você ainda não tem treinos registrados.</h4>
                  <p className="text-gray-400 text-lg md:text-xl">Que tal darmos o primeiro passo rumo à sua melhor versão hoje?</p>
                </div>
                <Link href="/workout/builder" className="bg-[#CCFF00] hover:bg-[#b8e600] text-[#121212] font-black text-lg py-4 px-8 rounded-2xl flex items-center gap-3 transition-transform hover:scale-105 active:scale-95 shadow-[0_0_20px_rgba(204,255,0,0.2)] mt-2">
                  <span className="material-symbols-outlined">add_circle</span>
                  VAMOS COMEÇAR
                </Link>
              </div>
            ) : (
              <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 items-start">
                {treinos.map((treino, index) => (
                  <WorkoutCard 
                    key={treino.id || index}
                    title={treino.tipo_exercicio || "Treino"}
                    duration={treino.duracao || 0}
                    calories={treino.calorias || 0}
                  />
                ))}
              </div>
            )}
          </section>

          {/* Section 3: Summary Cards (Grid of 3 columns on Desktop) */}
          <section className="grid grid-cols-1 md:grid-cols-3 gap-6 w-full">
            <div className="bg-surface-container-high p-6 md:p-8 rounded-3xl flex flex-col justify-between flex-1 border border-[#424242] neon-glow">
              <div className="flex justify-between items-start">
                <span className="p-3 bg-surface-container-lowest rounded-xl text-[#CCFF00] border border-[#424242]">
                  <span className="material-symbols-outlined text-2xl" style={{ fontVariationSettings: "'FILL' 1" }}>timer</span>
                </span>
                <span className="text-base font-black text-on-surface-variant uppercase tracking-widest">Semanal</span>
              </div>
              <div className="mt-8 md:mt-10">
                <p className="text-4xl md:text-5xl font-black text-white font-headline">245</p>
                <p className="text-on-surface-variant font-bold text-base md:text-lg">Minutos Ativos</p>
              </div>
            </div>
            
            <div className="bg-surface-container-low p-6 md:p-8 rounded-3xl flex flex-col justify-between flex-1 border border-[#424242]">
              <div className="flex justify-between items-start">
                <span className="p-3 bg-surface-container-lowest rounded-xl text-[#CCFF00] border border-[#424242] opacity-80">
                  <span className="material-symbols-outlined text-2xl" style={{ fontVariationSettings: "'FILL' 1" }}>bolt</span>
                </span>
                <span className="text-base font-black text-on-surface-variant uppercase tracking-widest">Streak</span>
              </div>
              <div className="mt-8 md:mt-10">
                <p className="text-4xl md:text-5xl font-black text-white font-headline">12</p>
                <p className="text-on-surface-variant font-bold text-base md:text-lg">Dias Seguidos</p>
              </div>
            </div>
            
            <div className="bg-[#0A0A0A] p-6 md:p-8 rounded-3xl flex flex-col justify-between flex-1 border border-[#424242]">
              <div className="flex justify-between items-start">
                <span className="p-3 bg-surface-container-low rounded-xl text-white border border-[#424242]">
                  <span className="material-symbols-outlined text-2xl" style={{ fontVariationSettings: "'FILL' 1" }}>favorite</span>
                </span>
                <span className="text-base font-black text-on-surface-variant uppercase tracking-widest">Total</span>
              </div>
              <div className="mt-8 md:mt-10">
                <p className="text-4xl md:text-5xl font-black text-white font-headline">4.2k</p>
                <p className="text-on-surface-variant font-bold text-base md:text-lg">Calorias Gastas</p>
              </div>
            </div>
          </section>

          {/* Section 2: Progress Metrics */}
          <section className="grid grid-cols-1 md:grid-cols-2 gap-8 w-full">
            {/* Frequência Semanal Chart */}
            <div className="bg-surface-container-low p-10 rounded-[2.5rem] border border-[#424242]">
              <div className="flex justify-between items-center mb-12">
                <h4 className="text-2xl font-black font-headline">Frequência</h4>
                <span className="text-base font-black neon-text">Meta: 5</span>
              </div>
              <div className="flex items-end justify-between h-48 gap-3">
                <div className="flex flex-col items-center gap-4 flex-1">
                  <div className="w-full bg-[#222] rounded-full h-24 relative overflow-hidden">
                    <div className="absolute bottom-0 w-full h-full bg-[#CCFF00]"></div>
                  </div>
                  <span className="text-base font-black text-on-surface-variant">S</span>
                </div>
                <div className="flex flex-col items-center gap-4 flex-1">
                  <div className="w-full bg-[#222] rounded-full h-32 relative overflow-hidden">
                    <div className="absolute bottom-0 w-full h-full bg-[#CCFF00]"></div>
                  </div>
                  <span className="text-base font-black text-on-surface-variant">T</span>
                </div>
                {/* ... existing bars for QUA, QUI, SEX, SAB, DOM. I'll just keep a few and the placeholder ... */}
                <div className="flex flex-col items-center gap-4 flex-1">
                  <div className="w-full bg-[#222] rounded-full h-12 relative overflow-hidden">
                    <div className="absolute bottom-0 w-full h-full bg-[#CCFF00]"></div>
                  </div>
                  <span className="text-base font-black text-on-surface-variant">Q</span>
                </div>
                <div className="flex flex-col items-center gap-4 flex-1">
                  <div className="w-full bg-[#222] rounded-full h-40 relative overflow-hidden">
                    <div className="absolute bottom-0 w-full h-full bg-[#CCFF00]"></div>
                  </div>
                  <span className="text-base font-black text-on-surface-variant">Q</span>
                </div>
                <div className="flex flex-col items-center gap-4 flex-1">
                  <div className="w-full bg-[#222] rounded-full h-28 relative overflow-hidden">
                    <div className="absolute bottom-0 w-full h-full bg-[#CCFF00]"></div>
                  </div>
                  <span className="text-base font-black text-on-surface-variant">S</span>
                </div>
                <div className="flex flex-col items-center gap-4 flex-1 opacity-20">
                  <div className="w-full bg-[#222] rounded-full h-8 relative overflow-hidden"></div>
                  <span className="text-base font-black text-on-surface-variant">S</span>
                </div>
                <div className="flex flex-col items-center gap-4 flex-1 opacity-20">
                  <div className="w-full bg-[#222] rounded-full h-8 relative overflow-hidden"></div>
                  <span className="text-base font-black text-on-surface-variant">D</span>
                </div>
              </div>
            </div>
            
            {/* Evolução de Peso Chart */}
            <div className="bg-surface-container-low p-10 rounded-[2.5rem] border border-[#424242]">
              <div className="flex justify-between items-center mb-12">
                <h4 className="text-2xl font-black font-headline">Peso</h4>
                <div className="flex gap-2">
                  <span className="px-4 py-1.5 bg-[#222] rounded-lg text-base font-black border border-[#424242]">82 → 79kg</span>
                </div>
              </div>
              <div className="relative h-48 flex items-end">
                <div className="absolute inset-0 flex flex-col justify-between opacity-10">
                  <div className="border-t border-[#424242] w-full"></div>
                  <div className="border-t border-[#424242] w-full"></div>
                  <div className="border-t border-[#424242] w-full"></div>
                </div>
                <div className="flex items-end justify-between w-full h-full px-6 relative z-10">
                  <div className="flex-1 flex flex-col items-center">
                    <div className="w-3 h-3 bg-white rounded-full mb-[120px]"></div>
                     <span className="text-base font-black text-on-surface-variant mt-3">J</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center">
                    <div className="w-3 h-3 bg-white rounded-full mb-[100px]"></div>
                     <span className="text-base font-black text-on-surface-variant mt-3">F</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center">
                    <div className="w-3 h-3 bg-white rounded-full mb-[95px]"></div>
                     <span className="text-base font-black text-on-surface-variant mt-3">M</span>
                  </div>
                  <div className="flex-1 flex flex-col items-center">
                    <div className="w-4 h-4 bg-[#CCFF00] rounded-full mb-[80px] shadow-[0_0_15px_#CCFF00]"></div>
                     <span className="text-base font-black neon-text mt-3">A</span>
                  </div>
                </div>
                <svg className="absolute bottom-12 left-0 w-full h-32 pointer-events-none" preserveAspectRatio="none" viewBox="0 0 400 100">
                  <path d="M50,20 L150,40 L250,45 L350,60" fill="none" opacity="0.4" stroke="#CCFF00" strokeDasharray="8 4" strokeWidth="3"></path>
                </svg>
              </div>
            </div>
          </section>

          {/* Health Metrics */}
          <section className="grid grid-cols-2 md:grid-cols-4 gap-4 md:gap-8 w-full">
            <div className="bg-[#1E1E1E] p-4 sm:p-6 md:p-8 rounded-3xl flex flex-col gap-2 md:gap-3 border border-neon neon-glow">
              <span className="text-base md:text-lg font-black uppercase tracking-widest text-on-surface-variant">Freq. Cardíaca</span>
              <div className="flex items-baseline gap-1 md:gap-2">
                <span className="text-3xl md:text-5xl font-black text-white font-headline">72</span>
                <span className="text-base md:text-lg font-black neon-text">BPM</span>
              </div>
            </div>
            <div className="bg-surface-container-low p-4 sm:p-6 md:p-8 rounded-3xl flex flex-col gap-2 md:gap-3 border border-[#424242]">
              <span className="text-base md:text-lg font-black uppercase tracking-widest text-on-surface-variant">Sono</span>
              <div className="flex items-baseline gap-1 md:gap-2">
                <span className="text-3xl md:text-5xl font-black text-white font-headline">7.5</span>
                <span className="text-base md:text-lg font-black text-on-surface-variant">Hrs</span>
              </div>
            </div>
            <div className="bg-surface-container-low p-4 sm:p-6 md:p-8 rounded-3xl flex flex-col gap-2 md:gap-3 border border-[#424242]">
              <span className="text-base md:text-lg font-black uppercase tracking-widest text-on-surface-variant">Água</span>
              <div className="flex items-baseline gap-1 md:gap-2">
                <span className="text-3xl md:text-5xl font-black text-white font-headline">1.8</span>
                <span className="text-base md:text-lg font-black text-on-surface-variant">L</span>
              </div>
            </div>
            <div className="bg-surface-container-low p-4 sm:p-6 md:p-8 rounded-3xl flex flex-col gap-2 md:gap-3 border border-[#424242]">
              <span className="text-base md:text-lg font-black uppercase tracking-widest text-on-surface-variant">Passos</span>
              <div className="flex items-baseline gap-1 md:gap-2">
                <span className="text-3xl md:text-5xl font-black text-white font-headline">8.4k</span>
                <span className="text-base md:text-lg font-black text-on-surface-variant">Hoje</span>
              </div>
            </div>
          </section>
        </div>
      </main>

      {/* Contextual FAB (Minimum 48x48 click area ensured by w-16 h-16) */}
      <Link href="/workout/ai-generator" className="fixed bottom-28 right-6 md:bottom-12 md:right-12 w-16 h-16 md:w-20 md:h-20 bg-[#CCFF00] text-[#161E00] rounded-2xl md:rounded-3xl shadow-[0_0_40px_rgba(204,255,0,0.4)] flex items-center justify-center kinetic-tap z-50 group border border-white/20">
        <span className="material-symbols-outlined font-bold text-3xl md:text-4xl transition-transform group-hover:rotate-90">smart_toy</span>
      </Link>

      {/* BottomNavBar for Mobile (Minimum 48px hit areas) */}
      <nav className="md:hidden fixed bottom-0 left-0 right-0 bg-[#0A0A0A] px-2 pt-3 pb-6 flex justify-around items-center z-40 border-t border-[#424242]">
        <Link href="/dashboard" className="flex flex-col items-center justify-center min-w-[64px] min-h-[48px] gap-1 neon-text">
          <span className="material-symbols-outlined" style={{ fontVariationSettings: "'FILL' 1" }}>dashboard</span>
          <span className="text-base font-black">Início</span>
        </Link>
        <Link href="/workout/builder" className="flex flex-col items-center justify-center min-w-[64px] min-h-[48px] gap-1 text-white opacity-40">
          <span className="material-symbols-outlined">fitness_center</span>
          <span className="text-base font-black">Treinos</span>
        </Link>
        <Link href="/library" className="flex flex-col items-center justify-center min-w-[64px] min-h-[48px] gap-1 text-white opacity-40">
          <span className="material-symbols-outlined">menu_book</span>
          <span className="text-base font-black">Biblio</span>
        </Link>
        <Link href="/profile" className="flex flex-col items-center justify-center min-w-[64px] min-h-[48px] gap-1 text-white opacity-40">
          <span className="material-symbols-outlined">person</span>
          <span className="text-base font-black">Perfil</span>
        </Link>
      </nav>
    </div>
  );
}
