<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/LegalTools.php';
require_once __DIR__ . '/../includes/ToolModels.php';

dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$ftUid    = dbnToolsFreeTierCheck('ask');
$engine   = ToolModels::engineForUser($ftUid, 'azure_mini');
$input    = dbnToolsJsonInput(25000);
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');

dbnToolsWithChargedTelemetry('ask', $language, $ftUid, function () use ($input, $language, $engine): array {
    $question = dbnToolsInjectDocContent($input, dbnToolsString($input, 'question', 4000, false));
    if (mb_strlen(trim($question), 'UTF-8') < 5) {
        dbnToolsAbort('Enter a question or select a document before running.', 422, 'empty_text');
    }
    return (new DbnLegalToolsService())->ask($question, $language, $engine);
});
