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

const SDK_URL = process.env.SDK_URL || 'http://ai-compliance-sdk:8090'
const DEFAULT_TENANT = process.env.DEFAULT_TENANT_ID || '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'

/**
 * Proxy: GET /api/sdk/v1/ucca/decision-tree → Go Backend GET /sdk/v1/ucca/decision-tree
 * Returns the decision tree definition (questions, structure)
 */
export async function GET(request: NextRequest) {
  const tenantID = request.headers.get('X-Tenant-ID') || DEFAULT_TENANT

  try {
    const response = await fetch(`${SDK_URL}/sdk/v1/ucca/decision-tree`, {
      headers: { 'X-Tenant-ID': tenantID },
    })

    if (!response.ok) {
      const errorText = await response.text()
      console.error('Decision tree GET error:', errorText)
      return NextResponse.json(
        { error: 'Backend error', details: errorText },
        { status: response.status }
      )
    }

    const data = await response.json()
    return NextResponse.json(data)
  } catch (error) {
    console.error('Decision tree proxy error:', error)
    return NextResponse.json(
      { error: 'Failed to connect to AI compliance backend' },
      { status: 503 }
    )
  }
}
