import { timingSafeEqual } from 'node:crypto'
import { NextRequest, NextResponse } from 'next/server'
import { settingsReport } from '@/lib/domain/settings-check'

export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'

/**
 * Yes/no report of which production settings are present (task T9).
 * Bearer CRON_SECRET, like the other internal jobs. Never returns a value.
 */
export async function GET(req: NextRequest) {
  const expected = process.env.CRON_SECRET || ''
  const supplied = req.headers.get('authorization') || ''
  const valid = expected.length >= 32 && Buffer.byteLength(supplied) === Buffer.byteLength(`Bearer ${expected}`)
    && timingSafeEqual(Buffer.from(supplied), Buffer.from(`Bearer ${expected}`))
  if (!valid) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  const res = NextResponse.json({ checked_at: new Date().toISOString(), ...settingsReport(process.env) })
  res.headers.set('Cache-Control', 'no-store')
  return res
}
