"use client"; import { useState, useEffect, useRef } from "react"; import { useAuth } from "@/context/AuthContext"; import { lmsApi, CMS_API_URL } from "@/lib/api"; import { Save, Shield, Mail, User as UserIcon, Trophy, Flame, Camera, Languages, FileText, LogOut, Trash2, Award } from "lucide-react"; export default function ProfilePage() { const { user, logout } = useAuth(); const [fullName, setFullName] = useState(user?.full_name || ""); const [email, setEmail] = useState(user?.email || ""); const [bio, setBio] = useState(user?.bio || ""); const [language, setLanguage] = useState(user?.language || "es"); const [avatarUrl, setAvatarUrl] = useState(user?.avatar_url || ""); const [saving, setSaving] = useState(false); const [uploading, setUploading] = useState(false); const [message, setMessage] = useState<{ type: 'success' | 'error', text: string } | null>(null); const [gamification, setGamification] = useState(null); const fileInputRef = useRef(null); useEffect(() => { if (user) { setFullName(user.full_name); setEmail(user.email); setBio(user.bio || ""); setLanguage(user.language || "es"); setAvatarUrl(user.avatar_url || ""); fetchGamification(); } }, [user]); const fetchGamification = async () => { if (!user) return; try { const data = await lmsApi.getGamification(user.id); setGamification(data); } catch (err) { console.error("Failed to fetch gamification:", err); } }; const getImageUrl = (path?: string) => { if (!path) return ''; if (path.startsWith('http')) return path; const cleanPath = path.startsWith('/uploads') ? path.replace('/uploads', '/assets') : path; const finalPath = cleanPath.startsWith('/') ? cleanPath : `/${cleanPath}`; return `${CMS_API_URL}${finalPath}`; }; const handleAvatarUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file || !user) return; try { setUploading(true); const res = await lmsApi.uploadAsset(file); setAvatarUrl(res.url); // Auto-save the new avatar URL await lmsApi.updateUser(user.id, { avatar_url: res.url }); setMessage({ type: 'success', text: '¡Avatar actualizado con éxito!' }); } catch (err) { console.error(err); setMessage({ type: 'error', text: 'Error al subir el avatar.' }); } finally { setUploading(false); } }; const handleSave = async (e: React.FormEvent) => { e.preventDefault(); if (!user) return; try { setSaving(true); setMessage(null); await lmsApi.updateUser(user.id, { full_name: fullName, bio, language, avatar_url: avatarUrl }); setMessage({ type: 'success', text: '¡Perfil actualizado con éxito!' }); } catch (err) { console.error(err); setMessage({ type: 'error', text: 'Error al actualizar el perfil.' }); } finally { setSaving(false); } }; if (!user) return null; return (

Mi Perfil

Personaliza tu identidad y sigue tu progreso de aprendizaje.

{/* Left Column: Stats & Profile */}
{/* Avatar Section */}
{avatarUrl ? ( {fullName} ) : ( {fullName.charAt(0)} )} {uploading && (
)}

{fullName}

Estudiante
{/* Gamification Stats */}

Nivel

{user.level || 1}

XP Total

{user.xp || 0}

{/* Badges Section */}

Logros Obtenidos

{gamification?.badges && gamification.badges.length > 0 ? (
{gamification.badges.map((badge: any) => (
{badge.icon_url ? ( {badge.name} ) : ( )}
{badge.name}
))}
) : (

¡Completa lecciones para ganar insignias!

)}
{/* Right Column: Settings Form */}
setFullName(e.target.value)} className="w-full bg-black/40 border border-white/10 rounded-2xl px-6 py-4 text-white focus:outline-none focus:border-blue-500/50 focus:ring-4 focus:ring-blue-500/5 transition-all outline-none" placeholder="Introduce tu nombre completo" required />