<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/DeepResearchAgent.php';

dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();

header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');

try {
    $raw = file_get_contents('php://input');
    if ($raw === false || strlen($raw) > 20000) {
        throw new DbnToolsHttpException('Request body unreadable or too large.', 413, 'body_too_large');
    }
    $input = json_decode((string)$raw, true);
    if (!is_array($input)) {
        throw new DbnToolsHttpException('Request body must be valid JSON.', 400, 'invalid_json');
    }

    $seedQuery    = mb_substr(trim((string)($input['query'] ?? '')), 0, 4000, 'UTF-8');
    $pastedText   = mb_substr(trim((string)($input['paste_text'] ?? '')), 0, 64000, 'UTF-8');
    $engine       = (string)($input['engine'] ?? 'azure_mini');
    $language     = dbnToolsNormalizeLanguage($input['language'] ?? 'en');
    $controls     = is_array($input['controls'] ?? null) ? $input['controls'] : [];
    $advocateRole = mb_substr(trim((string)($input['advocate_role'] ?? '')), 0, 200, 'UTF-8');
    $priorContext = is_array($input['prior_context'] ?? null) ? $input['prior_context'] : null;
    $branchNotes  = mb_substr(trim((string)($input['branch_notes'] ?? '')), 0, 1000, 'UTF-8');

    if ($seedQuery === '' && $pastedText === '') {
        throw new DbnToolsHttpException('Provide a query or pasted text.', 422, 'missing_seed');
    }

    $result = (new DbnDeepResearchAgent())->generateSubQPreview(
        $seedQuery,
        $pastedText,
        $engine,
        $language,
        $controls,
        $advocateRole,
        $priorContext,
        $branchNotes
    );

    echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

} catch (DbnToolsHttpException $e) {
    http_response_code($e->getCode() ?: 500);
    echo json_encode(['ok' => false, 'code' => $e->getSlug(), 'message' => $e->getMessage()]);
} catch (Throwable $e) {
    error_log('DBN generate-subq error: ' . $e->getMessage());
    http_response_code(500);
    echo json_encode(['ok' => false, 'code' => 'internal_error', 'message' => 'Could not generate sub-questions.']);
}
