async function uploadPhoto(input) { if (!input.files || !input.files[0]) return; const file = input.files[0]; if (file.size > 2097152) { ApexApp.showToast("Image must be smaller than 2MB!", true); return; } const fileExt = file.name.split('.').pop(); const filePath = `${user.id}-${Date.now()}.${fileExt}`; // Upload to 'avatars' bucket const { error: uploadError } = await supabase.storage .from('avatars') .upload(filePath, file, { upsert: true }); if (uploadError) { ApexApp.showToast(uploadError.message, true); return; } // Get public URL const { data: { publicUrl } } = supabase.storage .from('avatars') .getPublicUrl(filePath); // Update profiles table await supabase.from('profiles').update({ avatar_url: publicUrl }).eq('id', user.id); user.avatar_url = publicUrl; ApexApp.setUser(user); document.getElementById('avatar-preview').src = publicUrl; ApexApp.showToast("Profile image updated successfully!"); }