import { z } from 'zod'
import { requireAdmin } from '@/lib/auth-guard'
import { sendPushToUser } from '@/lib/push-server'

const schema = z.object({
  title: z.string().max(80).optional(),
  body: z.string().max(300).optional(),
  url: z.string().optional(),
})

export async function POST(req: Request) {
  const session = await requireAdmin()

  let input: z.infer<typeof schema> = {}
  try {
    const raw = await req.json()
    const parsed = schema.safeParse(raw)
    if (parsed.success) input = parsed.data
  } catch {
    // body is optional — use defaults
  }

  await sendPushToUser(session.userId, {
    title: input.title ?? 'Test push',
    body: input.body ?? 'Admin test notification',
    url: input.url ?? '/',
  })

  return new Response(null, { status: 204 })
}
