/**
 * Audit-Report PDF — Proxy (streamt die PDF-Bytes durch)
 * GET /api/sdk/v1/agent/snapshots/{snapshotId}/report/pdf
 *   → backend /api/compliance/agent/snapshots/{snapshotId}/report.pdf
 */

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,
  { params }: { params: Promise<{ snapshotId: string }> },
) {
  const { snapshotId } = await params
  try {
    const res = await fetch(
      `${BACKEND_URL}/api/compliance/agent/snapshots/${snapshotId}/report.pdf`,
      { signal: AbortSignal.timeout(120_000) },
    )
    if (!res.ok) {
      return NextResponse.json(
        { error: `PDF fehlgeschlagen (${res.status})` }, { status: res.status })
    }
    const buf = await res.arrayBuffer()
    return new NextResponse(buf, {
      status: 200,
      headers: {
        'Content-Type': 'application/pdf',
        'Content-Disposition':
          res.headers.get('content-disposition') ||
          'attachment; filename="audit-report.pdf"',
      },
    })
  } catch {
    return NextResponse.json({ error: 'PDF fehlgeschlagen' }, { status: 503 })
  }
}
