// import config from '@payload-config'
// import { getPayload } from 'payload'

// export const runtime = 'nodejs'

// export async function POST(req: Request) {
//   const secret = process.env.ADMIN_BOOTSTRAP_SECRET
//   const authHeader = req.headers.get('authorization')

//   if (!secret || authHeader !== `Bearer ${secret}`) {
//     return Response.json({ error: 'Unauthorized' }, { status: 401 })
//   }

//   const body = (await req.json().catch(() => null)) as
//     | {
//         email?: string
//         password?: string
//         name?: string
//       }
//     | null

//   const email = body?.email?.trim()
//   const password = body?.password
//   const name = body?.name?.trim() || 'Admin'

//   if (!email || !password) {
//     return Response.json({ error: 'Email and password are required.' }, { status: 400 })
//   }

//   const payload = await getPayload({ config })

//   const existing = await payload.find({
//     collection: 'users',
//     where: { email: { equals: email } },
//     limit: 1,
//     overrideAccess: true,
//   })

//   if (existing.totalDocs > 0) {
//     const user = existing.docs[0] as { id: string | number }
//     await payload.update({
//       collection: 'users',
//       id: user.id,
//       data: {
//         name,
//         password,
//         roles: ['admin'],
//       },
//       overrideAccess: true,
//     })

//     return Response.json({ status: 'updated', id: user.id })
//   }

//   const created = await payload.create({
//     collection: 'users',
//     data: {
//       name,
//       email,
//       password,
//       roles: ['admin'],
//     },
//     overrideAccess: true,
//   })

//   return Response.json({ status: 'created', id: created.id })
// }
