<?php
declare(strict_types=1);

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

dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$ftUid = dbnToolsFreeTierCheck('timeline');
$ftRemaining = dbnToolsFreeTierDeduct($ftUid, 'timeline');
if ($ftRemaining >= 0) { header('X-Credits-Remaining: ' . $ftRemaining); }
$input = dbnToolsJsonInput(400000);
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');

dbnToolsWithTelemetry('timeline', $language, function () use ($input, $language): array {
    $text = dbnToolsString($input, 'text', 128000);

    $validEngines = ['azure_mini', 'azure_full', 'gpu'];
    $engine = in_array((string)($input['engine'] ?? ''), $validEngines, true)
        ? (string)$input['engine'] : 'azure_mini';

    $validFocus = ['all', 'deadlines', 'hearings', 'cps'];
    $focus = in_array((string)($input['focus'] ?? ''), $validFocus, true)
        ? (string)$input['focus'] : 'all';

    $confidenceFilter = (string)($input['confidence_filter'] ?? '') === 'high_medium'
        ? 'high_medium' : 'all';

    $includeRelative   = ($input['include_relative']   ?? true) !== false;
    $includeBackground = ($input['include_background'] ?? true) !== false;
    $userNotes         = dbnToolsString($input, 'user_notes', 2000, false);

    return (new DbnLegalToolsService())->timeline($text, $language, $engine, $focus, $confidenceFilter, $includeRelative, $includeBackground, $userNotes);
});
