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

const SDK_URL = process.env.SDK_URL || 'http://ai-compliance-sdk:8090'

export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id } = await params
    const resp = await fetch(`${SDK_URL}/sdk/v1/payment-compliance/tender/${id}`)
    return NextResponse.json(await resp.json())
  } catch {
    return NextResponse.json({ error: 'Failed' }, { status: 500 })
  }
}

export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id } = await params
    const { searchParams } = new URL(request.url)
    const action = searchParams.get('action') || 'extract'
    const resp = await fetch(`${SDK_URL}/sdk/v1/payment-compliance/tender/${id}/${action}`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
    })
    return NextResponse.json(await resp.json(), { status: resp.status })
  } catch {
    return NextResponse.json({ error: 'Failed' }, { status: 500 })
  }
}
