/**
 * Agent Doc-Check Proxy — Multi-URL document verification
 * POST: start check, GET: poll status
 */

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

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

export async function POST(request: NextRequest) {
  try {
    const body = await request.text()
    const response = await fetch(`${BACKEND_URL}/api/compliance/agent/doc-check`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body,
      signal: AbortSignal.timeout(30000),
    })
    const data = await response.json()
    return NextResponse.json(data, { status: response.status })
  } catch (error) {
    return NextResponse.json({ error: 'Pruefung konnte nicht gestartet werden' }, { status: 503 })
  }
}

export async function GET(request: NextRequest) {
  const checkId = request.nextUrl.searchParams.get('check_id')
  if (!checkId) return NextResponse.json({ error: 'check_id required' }, { status: 400 })
  try {
    const response = await fetch(
      `${BACKEND_URL}/api/compliance/agent/doc-check/${checkId}`,
      { signal: AbortSignal.timeout(10000) },
    )
    const data = await response.json()
    return NextResponse.json(data)
  } catch {
    return NextResponse.json({ error: 'Status-Abfrage fehlgeschlagen' }, { status: 503 })
  }
}
