import { NextRequest, NextResponse } from 'next/server'
import { auth, getSanitizedHeaders } from '@/lib/auth'
import { prisma } from '@innungsapp/shared'

export async function POST(req: NextRequest) {
  const session = await auth.api.getSession({ headers: await getSanitizedHeaders(req.headers) })
  if (!session?.user) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  const { token } = await req.json()
  if (!token || typeof token !== 'string') {
    return NextResponse.json({ error: 'Invalid token' }, { status: 400 })
  }

  // Store push token on the member record
  await prisma.member.updateMany({
    where: { userId: session.user.id },
    data: { pushToken: token },
  })

  return NextResponse.json({ success: true })
}
