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

const SDK_URL = process.env.SDK_URL || 'http://ai-compliance-sdk:8090'
const DEFAULT_TENANT_ID = process.env.DEFAULT_TENANT_ID || '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'

export async function GET(request: NextRequest) {
  try {
    const { searchParams } = new URL(request.url)
    const qs = searchParams.toString()
    const url = `${SDK_URL}/sdk/v1/regulatory-news${qs ? `?${qs}` : ''}`

    const response = await fetch(url, {
      headers: {
        'Content-Type': 'application/json',
        'X-Tenant-ID': request.headers.get('X-Tenant-ID') || DEFAULT_TENANT_ID,
      },
    })

    if (!response.ok) {
      return NextResponse.json({ error: 'SDK error' }, { status: response.status })
    }
    return NextResponse.json(await response.json())
  } catch {
    return NextResponse.json({ error: 'Connection failed' }, { status: 503 })
  }
}
