/**
 * Compliance-Check SSE-Proxy
 * GET /api/sdk/v1/agent/compliance-check/{check_id}/stream
 *   → backend /api/compliance/agent/compliance-check/{check_id}/stream
 *
 * Reicht den text/event-stream-Body unmodifiziert durch (progressive
 * topic-/progress-Events fürs Frontend). Additiv zum Polling.
 */

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<{ check_id: string }> },
) {
  const { check_id } = await params
  try {
    const response = await fetch(
      `${BACKEND_URL}/api/compliance/agent/compliance-check/${check_id}/stream`,
      { signal: AbortSignal.timeout(1_800_000) }, // 30 min
    )
    return new NextResponse(response.body, {
      status: response.status,
      headers: {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'X-Accel-Buffering': 'no',
      },
    })
  } catch {
    return NextResponse.json(
      { error: 'SSE-Stream zum Backend fehlgeschlagen' },
      { status: 503 },
    )
  }
}
