import { prisma } from '@/lib/prisma'
import { requireUser } from '@/lib/auth'

export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    await requireUser()
  } catch {
    return new Response(null, { status: 401 })
  }

  const { id } = await params

  const user = await prisma.user.findUnique({
    where: { id },
    select: { avatar_data: true },
  })

  if (!user?.avatar_data) {
    return new Response(null, { status: 404 })
  }

  return new Response(user.avatar_data, {
    headers: {
      'Content-Type': 'image/webp',
      'Cache-Control': 'private, max-age=3600',
    },
  })
}
