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

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

export async function GET(req: NextRequest) {
  const qs = req.nextUrl.searchParams.toString()
  const res = await fetch(`${API}/email${qs ? `?${qs}` : ''}`, { cache: 'no-store' })
  return NextResponse.json(await res.json(), { status: res.status })
}

export async function POST(req: NextRequest) {
  // POST /api/email/send is handled via /api/email/send route;
  // this POST creates a raw outbound email record
  const body = await req.json()
  const res = await fetch(`${API}/email/send`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(body),
  })
  return NextResponse.json(await res.json(), { status: res.status })
}
