import { prisma } from '~/server/utils/prisma' export default defineEventHandler(async (event) => { const userId = getRouterParam(event, 'id') if (!userId) { throw createError({ statusCode: 400, message: 'User ID is required' }) } let profile: any = null try { profile = await prisma.profile.findFirst({ where: { OR: [ { userId: userId }, { user: { username: userId } } ] }, include: { user: { include: { _count: { select: { canvases: true, followers: true, following: true } } } } } }) if (!profile) { const user = await prisma.user.findFirst({ where: { OR: [{ id: userId }, { username: userId }] } }) if (user) { profile = await prisma.profile.create({ data: { userId: user.id, displayName: user.username, avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${user.id}` }, include: { user: { include: { _count: { select: { canvases: true, followers: true, following: true } } } } } }) } } } catch (e: any) { console.warn(`[Profile API] Database unreachable (${e.message}), returning dev profile for ${userId}`) } if (!profile) { profile = { userId, displayName: userId.length > 20 ? 'User' : userId, avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${userId}`, bio: '', user: { _count: { canvases: 0, followers: 0, following: 0 } } } } return { profile: { ...profile, canvasCount: profile.user?._count?.canvases || 0, followerCount: profile.user?._count?.followers || 0, followingCount: profile.user?._count?.following || 0 } } })