/**
 * Text Extraction Proxy — extract text from a URL via consent-tester
 * POST: { url: string } -> { text, word_count, title, error }
 */

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/extract-text`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body,
      signal: AbortSignal.timeout(120000),
    })
    const data = await response.json()
    return NextResponse.json(data, { status: response.status })
  } catch (error) {
    return NextResponse.json(
      { text: '', word_count: 0, title: '', error: 'Text-Extraktion fehlgeschlagen' },
      { status: 503 },
    )
  }
}
