import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import Layout from "@/components/Layout"; import { Button } from "@/components/ui/button"; import { useAuth } from "@/contexts/AuthContext"; import { useArmTheme } from "@/contexts/ArmThemeContext"; import { aethexToast } from "@/lib/aethex-toast"; import { supabase } from "@/lib/supabase"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import LoadingScreen from "@/components/LoadingScreen"; import { BookOpen, Award, Users, TrendingUp, CheckCircle, AlertCircle, Star, Clock, Target, ArrowRight, Zap, } from "lucide-react"; import { CoursesWidget } from "@/components/CoursesWidget"; import { MentorshipWidget } from "@/components/MentorshipWidget"; import { AchievementsWidget } from "@/components/AchievementsWidget"; const getApiBase = () => typeof window !== "undefined" ? window.location.origin : ""; export default function FoundationDashboard() { const navigate = useNavigate(); const { user, loading: authLoading } = useAuth(); const { theme } = useArmTheme(); const [activeTab, setActiveTab] = useState("overview"); const [courses, setCourses] = useState([]); const [mentorships, setMentorships] = useState([]); const [achievements, setAchievements] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { if (!authLoading && user) { loadDashboardData(); } }, [user, authLoading]); const loadDashboardData = async () => { try { setLoading(true); const { data: { session }, } = await supabase.auth.getSession(); const token = session?.access_token; if (!token) throw new Error("No auth token"); try { const coursesRes = await fetch(`${apiBase}/api/foundation/courses`, { headers: { Authorization: `Bearer ${token}` }, }); if ( coursesRes.ok && coursesRes.headers.get("content-type")?.includes("application/json") ) { const data = await coursesRes.json(); setCourses(Array.isArray(data) ? data : []); } } catch (err: any) { // Silently ignore API errors - dashboard will render with empty data } try { const apiBase = getApiBase(); if (!apiBase) { console.warn("[Foundation] No API base available"); return; } const mentorRes = await fetch(`${apiBase}/api/foundation/mentorships`, { headers: { Authorization: `Bearer ${token}` }, }); if ( mentorRes.ok && mentorRes.headers.get("content-type")?.includes("application/json") ) { const data = await mentorRes.json(); setMentorships(data.as_mentee || []); } } catch (err: any) { // Silently ignore API errors - dashboard will render with empty data } } catch (error: any) { // Silently ignore errors } finally { setLoading(false); } }; if (authLoading || loading) { return ; } if (!user) { return (

Join FOUNDATION

Learn from industry experts and mentors

); } const enrolledCourses = courses.filter((c: any) => c.userEnrollment); const completedCourses = enrolledCourses.filter( (c: any) => c.userEnrollment?.status === "completed", ); const activeMentor = mentorships.find((m: any) => m.status === "accepted"); return (
{/* Header */}

FOUNDATION University

Learn, grow, and earn achievement badges

{/* Quick Stats */}

Courses Enrolled

{enrolledCourses.length}

Completed

{completedCourses.length}

Achievements

{achievements.length}

Mentor

{activeMentor ? "✓" : "—"}

{/* Tabs */} Overview Courses Mentorship Achievements {/* Overview Tab */} {/* Mentorship Widget */} ({ id: m.id, mentor: { id: m.mentor?.id, name: m.mentor?.full_name, avatar: m.mentor?.avatar_url, expertise: m.mentor?.role_title, }, status: m.status, connectedSince: m.accepted_at ? new Date(m.accepted_at).toLocaleDateString() : null, lastSession: m.last_session_date, nextSession: m.next_session_date, }))} title="Your Mentorship" description="Connect with industry experts" accentColor="red" onFindMentor={() => navigate("/mentors")} /> {/* Courses Widget */} ({ id: c.id, title: c.title, description: c.description, category: c.category, progress: c.userEnrollment?.progress_percent || 0, status: c.userEnrollment?.status || "not_started", duration: c.duration, lessons_total: c.lessons_total, lessons_completed: c.userEnrollment?.lessons_completed || 0, instructor: c.instructor?.full_name, thumbnail_url: c.thumbnail_url, }))} title="Continue Learning" description="Resume your courses" accentColor="red" onViewCourse={(courseId) => navigate(`/courses/${courseId}`)} /> {/* Courses Tab */} ({ id: c.id, title: c.title, description: c.description, category: c.category, progress: c.userEnrollment?.progress_percent || 0, status: c.userEnrollment?.status || "not_started", duration: c.duration, lessons_total: c.lessons_total, lessons_completed: c.userEnrollment?.lessons_completed || 0, instructor: c.instructor?.full_name, thumbnail_url: c.thumbnail_url, }))} title="My Courses" description="All your enrollments" accentColor="red" onViewCourse={(courseId) => navigate(`/courses/${courseId}`)} /> {/* Mentorship Tab */} ({ id: m.id, mentor: { id: m.mentor?.id, name: m.mentor?.full_name, avatar: m.mentor?.avatar_url, expertise: m.mentor?.role_title, }, status: m.status, connectedSince: m.accepted_at ? new Date(m.accepted_at).toLocaleDateString() : null, lastSession: m.last_session_date, nextSession: m.next_session_date, }))} title="Mentorship" description="Your mentor relationships" accentColor="red" onFindMentor={() => navigate("/mentors")} /> {/* Achievements Tab */} ({ id: a.id, name: a.name, description: a.description, icon: a.icon, rarity: a.rarity || "common", earnedDate: a.earned_date, category: a.category, }))} title="Your Achievements" description="Badges earned through learning and growth" accentColor="red" />
); }