import { apiRoute } from '@/lib/api/handler'
import { listHunts } from '@/lib/services/risk-hunter-service'

export const dynamic = 'force-dynamic'

/**
 * Everything the hunter has found, newest first.
 *
 * Flattened across runs because an operator asks "what has been found", not "what did run
 * seven find" — the run is still on each finding for anyone who needs it.
 */
export const GET = apiRoute({ permission: 'riskhunter.read' }, async ({ searchParams }) => {
  const limit = Math.min(30, Math.max(1, Number(searchParams.get('limit')) || 10))
  const hunts = await listHunts(limit)

  return {
    hunts,
    findings: hunts.flatMap((hunt) =>
      hunt.findings.map((finding) => ({ ...finding, runCode: hunt.code, mode: hunt.mode })),
    ),
    evidenceKind: 'simulated',
  }
})
