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

const API = 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(`${API}/documents/${id}/content`, { cache: 'no-store' })
  if (!res.ok) return NextResponse.json({ message: 'File not found' }, { status: res.status })

  const contentType = res.headers.get('content-type') ?? 'application/octet-stream'
  const disposition = res.headers.get('content-disposition') ?? 'inline'
  const buffer = await res.arrayBuffer()

  return new NextResponse(buffer, {
    status: 200,
    headers: {
      'Content-Type': contentType,
      'Content-Disposition': disposition,
    },
  })
}
