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 { useAethexToast } from "@/hooks/use-aethex-toast"; import { supabase } from "@/lib/supabase"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Switch } from "@/components/ui/switch"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Separator } from "@/components/ui/separator"; import LoadingScreen from "@/components/LoadingScreen"; import { User, Settings, Activity, Globe, Shield, Bell, Database, Monitor, Network, Zap, TrendingUp, Users, Server, Eye, Link2, Wifi, Cloud, Lock, ChevronRight, RefreshCw, AlertTriangle, CheckCircle, XCircle, Circle, } from "lucide-react"; interface SiteStatus { name: string; url: string; status: "online" | "offline" | "maintenance"; lastCheck: string; responseTime: number; version: string; } interface CrossSiteCommunication { siteName: string; lastSync: string; dataExchanged: number; status: "connected" | "disconnected" | "syncing"; } export default function Profile() { const navigate = useNavigate(); const { user, profile, loading: authLoading, updateProfile } = useAuth(); const { success: toastSuccess, error: toastError } = useAethexToast(); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); // Profile settings state const [profileData, setProfileData] = useState({ displayName: "", bio: "", company: "", location: "", website: "", githubUsername: "", twitterUsername: "", linkedinUrl: "", }); // Notification settings const [notifications, setNotifications] = useState({ emailNotifications: true, pushNotifications: true, projectUpdates: true, marketingEmails: false, securityAlerts: true, }); // Privacy settings const [privacy, setPrivacy] = useState({ profileVisibility: "public", showEmail: false, showProjects: true, allowAnalytics: true, }); // Overseer dashboard data const [overseerData, setOverseerData] = useState({ systemHealth: 98.5, activeUsers: 1247, totalProjects: 3592, serverLoad: 23, dataProcessed: 15.7, // GB apiCalls: 45231, errorRate: 0.03, }); // Cross-site communication data const [crossSiteData, setCrossSiteData] = useState([ { siteName: "AeThex Labs", lastSync: "2 minutes ago", dataExchanged: 1.2, status: "connected", }, { siteName: "Development Portal", lastSync: "5 minutes ago", dataExchanged: 0.8, status: "syncing", }, { siteName: "Community Hub", lastSync: "12 minutes ago", dataExchanged: 2.1, status: "connected", }, ]); // Site monitoring data const [siteStatuses, setSiteStatuses] = useState([ { name: "Main Site", url: "https://core.aethex.biz", status: "online", lastCheck: "Just now", responseTime: 245, version: "1.0.0", }, { name: "API Gateway", url: "https://api.aethex.biz", status: "online", lastCheck: "30 seconds ago", responseTime: 123, version: "2.1.3", }, { name: "Labs Portal", url: "https://labs.aethex.biz", status: "maintenance", lastCheck: "2 minutes ago", responseTime: 0, version: "1.5.2", }, ]); const currentStreak = profile?.current_streak ?? 0; const longestStreak = profile?.longest_streak ?? currentStreak; useEffect(() => { if (!authLoading && !user) { navigate("/login"); return; } if (profile) { setProfileData({ displayName: profile.full_name || "", bio: profile.bio || "", company: (profile as any).company || "", location: profile.location || "", website: (profile as any).website || "", githubUsername: (profile as any).github_username || "", twitterUsername: (profile as any).twitter_username || "", linkedinUrl: profile.linkedin_url || "", }); } setIsLoading(false); }, [authLoading, user, profile, navigate]); const handleProfileSave = async () => { if (!user) return; setIsSaving(true); console.log("Starting profile save...", { user: user.id, profile: !!profile, profileData, }); try { // Check if profile exists, create if it doesn't if (!profile) { console.log("No profile exists, creating one..."); try { // Try using the AuthContext updateProfile (which works with mock too) await updateProfile({ username: profileData.displayName || user.email?.split("@")[0] || `user_${Date.now()}`, full_name: profileData.displayName, bio: profileData.bio, location: profileData.location, linkedin_url: profileData.linkedinUrl, github_url: profileData.githubUsername ? `https://github.com/${profileData.githubUsername}` : null, twitter_url: profileData.twitterUsername ? `https://twitter.com/${profileData.twitterUsername}` : null, user_type: "game_developer", experience_level: "beginner", level: 1, total_xp: 0, } as any); console.log("Profile created successfully via AuthContext"); } catch (authError) { console.warn( "AuthContext creation failed, trying direct database:", authError, ); // Fallback to direct database call const { data, error } = await supabase .from("user_profiles") .insert({ id: user.id, username: profileData.displayName || user.email?.split("@")[0] || `user_${Date.now()}`, user_type: "game_developer", experience_level: "beginner", full_name: profileData.displayName, bio: profileData.bio, location: profileData.location, linkedin_url: profileData.linkedinUrl, github_url: profileData.githubUsername ? `https://github.com/${profileData.githubUsername}` : null, twitter_url: profileData.twitterUsername ? `https://twitter.com/${profileData.twitterUsername}` : null, level: 1, total_xp: 0, created_at: new Date().toISOString(), updated_at: new Date().toISOString(), }) .select() .single(); if (error) { console.error("Error creating profile:", error); throw error; } console.log("Profile created successfully via direct DB:", data); } } else { console.log("Updating existing profile..."); await updateProfile({ full_name: profileData.displayName, bio: profileData.bio, location: profileData.location, linkedin_url: profileData.linkedinUrl, github_url: profileData.githubUsername ? `https://github.com/${profileData.githubUsername}` : null, twitter_url: profileData.twitterUsername ? `https://twitter.com/${profileData.twitterUsername}` : null, updated_at: new Date().toISOString(), } as any); } toastSuccess({ title: "Profile Updated", description: "Your profile has been successfully updated.", }); } catch (error: any) { console.error("Profile save error:", error); toastError({ title: "Update Failed", description: error.message || "Failed to update profile. Please try again.", }); } finally { setIsSaving(false); } }; const getStatusIcon = (status: string) => { switch (status) { case "online": case "connected": return ; case "offline": case "disconnected": return ; case "maintenance": case "syncing": return ; default: return ; } }; const getStatusColor = (status: string) => { switch (status) { case "online": case "connected": return "bg-green-500"; case "offline": case "disconnected": return "bg-red-500"; case "maintenance": case "syncing": return "bg-yellow-500"; default: return "bg-gray-400"; } }; if (authLoading || isLoading) { return ; } return (
{/* Header */}
{profile?.full_name?.[0] || user?.email?.[0]?.toUpperCase()}

{profile?.full_name || "User Profile"}

{user?.email}

Current streak: {currentStreak}d Longest streak: {longestStreak}d
Profile Settings Overseer Network {/* Profile Settings Tab */} Profile Information Update your profile information and social links.
setProfileData({ ...profileData, displayName: e.target.value, }) } className="bg-slate-900/50 border-slate-600 text-white" />
setProfileData({ ...profileData, company: e.target.value, }) } className="bg-slate-900/50 border-slate-600 text-white" />
setProfileData({ ...profileData, location: e.target.value, }) } className="bg-slate-900/50 border-slate-600 text-white" />
setProfileData({ ...profileData, website: e.target.value, }) } className="bg-slate-900/50 border-slate-600 text-white" />