import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { setPrice } from '@/lib/admin-prices'

export async function POST(request: Request) {
  const session = await getSession()
  if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
  let b: Record<string, unknown> = {}
  try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
  const service = String(b.service ?? '')
  const value = Number(b.value)
  if (!service || !(value >= 0)) return Response.json({ success: false, error: 'invalidRequest' })
  const ok = await setPrice(service, value)
  return Response.json({ success: ok })
}
