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'

/**
 * Proxy: POST /api/sdk/v1/ucca/assess-enriched → Go Backend POST /sdk/v1/ucca/assess-enriched
 * Accepts { intake, company_profile? } and returns enriched assessment with obligations + hints.
 */
export async function POST(request: NextRequest) {
  try {
    const body = await request.json()

    const response = await fetch(`${SDK_URL}/sdk/v1/ucca/assess-enriched`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Tenant-ID': request.headers.get('X-Tenant-ID') || DEFAULT_TENANT_ID,
      },
      body: JSON.stringify(body),
    })

    if (!response.ok) {
      const errorText = await response.text()
      console.error('UCCA assess-enriched error:', errorText)
      return NextResponse.json(
        { error: 'UCCA backend error', details: errorText },
        { status: response.status }
      )
    }

    const data = await response.json()
    return NextResponse.json(data, { status: 201 })
  } catch (error) {
    console.error('Failed to call UCCA assess-enriched:', error)
    return NextResponse.json(
      { error: 'Failed to connect to UCCA backend' },
      { status: 503 }
    )
  }
}
