/**
 * Snapshot-Liste (Historie)
 * GET /api/sdk/v1/agent/snapshots?domain=&limit=
 *   → backend /api/compliance/agent/snapshots
 *
 * Ohne domain: alle letzten Snapshots (Historie zum Durchklicken).
 */

import { NextRequest, NextResponse } from 'next/server'

const BACKEND_URL =
  process.env.BACKEND_API_URL || process.env.BACKEND_URL ||
  'http://backend-compliance:8002'

export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url)
  const domain = searchParams.get('domain') || ''
  const limit = searchParams.get('limit') || '50'
  try {
    const response = await fetch(
      `${BACKEND_URL}/api/compliance/agent/snapshots`
        + `?domain=${encodeURIComponent(domain)}&limit=${encodeURIComponent(limit)}`,
      { signal: AbortSignal.timeout(30_000) },
    )
    const data = await response.json()
    return NextResponse.json(data, { status: response.status })
  } catch {
    return NextResponse.json(
      { error: 'Snapshot-Liste zum Backend fehlgeschlagen', snapshots: [] },
      { status: 503 },
    )
  }
}
