import { NextRequest, NextResponse } from 'next/server' const PYTHON_API_URL = process.env.PYTHON_API_URL || 'http://localhost:5000' // API route configuration - dynamic for POST requests export const dynamic = 'force-dynamic' export const runtime = 'nodejs' export async function POST(request: NextRequest) { try { const body = await request.json() const { budget, user_inputs, page, limit, performance_category } = body // Create AbortController for timeout const controller = new AbortController() const timeoutId = setTimeout(() => controller.abort(), 300000) // 300 second (5 minute) timeout for CSP algorithm with larger budgets try { // Prepare request body for Python backend const pythonRequestBody: any = { budget, user_inputs, page: page !== undefined ? page : 0, limit: limit !== undefined ? limit : 10, } // Add performance category if specified if (performance_category && performance_category !== 'all') { pythonRequestBody.performance_category = performance_category } // Call Python backend const response = await fetch(`${PYTHON_API_URL}/api/csp`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(pythonRequestBody), signal: controller.signal, }) clearTimeout(timeoutId) // Check content-type to ensure we're getting JSON, not HTML const contentType = response.headers.get('content-type') || '' const isJSON = contentType.includes('application/json') if (!response.ok) { let errorMessage = 'Failed to get recommendations from algorithm' try { const errorText = await response.text() console.error('Python API error:', errorText) // If response is HTML (error page), provide helpful message if (!isJSON || errorText.trim().startsWith('