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

const LEGAL_SERVICE = process.env.LEGAL_SERVICE_URL ?? 'http://localhost:3200'

export async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params
  const res = await fetch(`${LEGAL_SERVICE}/invoices/${id}/pdf`, { cache: 'no-store' })
  if (!res.ok) return NextResponse.json({ message: 'PDF generation failed' }, { status: res.status })

  const pdf = await res.arrayBuffer()
  return new NextResponse(pdf, {
    status: 200,
    headers: {
      'Content-Type':        'application/pdf',
      'Content-Disposition': res.headers.get('Content-Disposition') ?? `attachment; filename="invoice.pdf"`,
    },
  })
}
