<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
$_POST['job_id'] = $argv[1] ?? '335';
ob_start();
try {
    include __DIR__ . '/../app/api/generate_next_question.php';
} catch (Throwable $e) {
    ob_end_clean();
    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage(),
        'file' => $e->getFile(),
        'line' => $e->getLine(),
    ], JSON_PRETTY_PRINT);
    exit(1);
}
$out = ob_get_clean();
$decoded = json_decode($out, true);
if ($decoded === null) {
    echo json_encode([
        'status' => 'invalid_json',
        'json_error' => json_last_error_msg(),
        'raw_head' => substr($out, 0, 800),
    ], JSON_PRETTY_PRINT);
    exit(1);
}
echo json_encode($decoded, JSON_PRETTY_PRINT), PHP_EOL;
