'use client'; import React, { useEffect, useState } from 'react'; import { motion } from 'framer-motion'; import { Activity, User, Clock, ShieldAlert, Plus, Users, UserPlus } from 'lucide-react'; import Link from 'next/link'; import { AuthProvider, useAuth } from '@/lib/auth'; export default function DashboardPageWrap() { return ( ); } function DashboardPage() { const { user } = useAuth(); const [greeting, setGreeting] = useState(''); const [stats, setStats] = useState({ totalMenus: 0, totalSessions: 0, // All time sessionsToday: 0, recentMenus: [] as any[], recentRecaps: [] as any[], linkedClients: [] as any[] }); // Add Client State const [isAddingClient, setIsAddingClient] = useState(false); const [clientIdToAdd, setClientIdToAdd] = useState(''); const [addClientStatus, setAddClientStatus] = useState(''); const loadData = React.useCallback(async () => { if (!user) return; // Security Check if (user.role !== 'COACH') { window.location.href = '/'; return; } try { const headers = { 'x-user-id': user.id }; // Fetch Recaps const resRecaps = await fetch('/api/recap', { headers }); const recaps = await resRecaps.json(); // Fetch Menus const resMenus = await fetch('/api/menus', { headers }); const menus = await resMenus.json(); // Fetch Linked Clients const resClients = await fetch(`/api/users?coachId=${user.id}`); const clients = await resClients.json(); if (Array.isArray(recaps) && Array.isArray(menus)) { const today = new Date().toDateString(); const todaysRecaps = recaps.filter((r: any) => new Date(r.completed_at).toDateString() === today); setStats({ totalMenus: menus.length, totalSessions: recaps.length, sessionsToday: todaysRecaps.length, recentMenus: menus.sort((a: any, b: any) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()).slice(0, 5), recentRecaps: recaps.sort((a: any, b: any) => new Date(b.completed_at).getTime() - new Date(a.completed_at).getTime()).slice(0, 5), linkedClients: Array.isArray(clients) ? clients : [] }); } } catch (e) { console.error("Failed to load dashboard stats", e); } }, [user]); useEffect(() => { const hour = new Date().getHours(); if (hour < 12) setGreeting('Good Morning'); else if (hour < 18) setGreeting('Good Afternoon'); else setGreeting('Good Evening'); loadData(); }, [user, loadData]); const handleAddClient = async (e: React.FormEvent) => { e.preventDefault(); if (!user || !clientIdToAdd) return; setAddClientStatus('Linking...'); try { const res = await fetch('/api/coach/link-client', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ coachId: user.id, clientId: clientIdToAdd }) }); const data = await res.json(); if (res.ok) { setAddClientStatus('Client Linked!'); setClientIdToAdd(''); setIsAddingClient(false); // Refresh data await loadData(); } else { setAddClientStatus(data.error || 'Failed to link'); } } catch (e) { setAddClientStatus('Error linking client'); } }; // Staggered animation variants const container = { hidden: { opacity: 0 }, show: { opacity: 1, transition: { staggerChildren: 0.1 } } }; const item = { hidden: { opacity: 0, y: 20 }, show: { opacity: 1, y: 0 } }; return (

{greeting}, {user?.name || 'Coach'}.

SYSTEM ACTIVE

NEW PROGRAM
} variant={item} /> } variant={item} /> } variant={item} />

Live Activity Feed

Now System monitoring active...
{/* My Clients Section */}

My Clients

{isAddingClient && (
setClientIdToAdd(e.target.value)} className="bg-white border border-zinc-200 rounded-lg px-4 py-2 text-sm w-48 font-mono" /> {addClientStatus && {addClientStatus}}
)}
{stats.linkedClients.map((client: any) => (
{client.name.charAt(0)}
{client.name}
ID: {client.id}
))} {stats.linkedClients.length === 0 && (
No clients linked yet. Add one above.
)}
{/* Recent Menus List */}

Recent Programs

{stats.recentMenus.map((menu: any) => ( ))} {stats.recentMenus.length === 0 && ( )}
Program Name Client Created At Exercises Action
{menu.name} {menu.assigned_client ? ( {menu.assigned_client.name} ) : ( Unassigned )} {new Date(menu.created_at).toLocaleDateString()} {(() => { if (!menu.exercises) return 0; if (Array.isArray(menu.exercises)) return menu.exercises.length; try { return JSON.parse(menu.exercises as string).length; } catch { return 0; } })()} exercises View Details
No programs created yet.
{/* Recent Recaps List */}

Recent Activity Reports

{stats.recentRecaps.map((recap: any) => ( ))} {stats.recentRecaps.length === 0 && ( )}
Date Client Program Status Action
{new Date(recap.completed_at).toLocaleString()}
{recap.user?.name ? recap.user.name[0] : '?'}
{recap.user?.name || 'Unknown'}
{recap.training_menus?.name || `Menu #${recap.menu_id}`} COMPLETED View Report
No activity recorded yet.
); } function StatsCard({ title, value, icon, variant }: any) { return (
{icon}

{title}

{value}
); }