<?php
declare(strict_types=1);

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

dbnToolsRequireMethod('POST');
dbnToolsRequireAuth();
$input = dbnToolsJsonInput(12000);
$language = dbnToolsNormalizeLanguage($input['language'] ?? 'en');

dbnToolsWithTelemetry('search', $language, function () use ($input, $language): array {
    $query        = dbnToolsString($input, 'query', 2000);
    $limit        = max(1, min(10, (int)($input['limit'] ?? 6)));
    $temporalMode = in_array($input['temporal_mode'] ?? '', ['legal_conservative', 'disabled'], true)
        ? $input['temporal_mode']
        : 'disabled';
    $asOfDate = isset($input['as_of_date']) && preg_match('/^\d{4}(-\d{2}(-\d{2})?)?$/', $input['as_of_date'])
        ? $input['as_of_date']
        : null;
    return (new DbnLegalToolsService())->search($query, $language, $limit, $temporalMode, $asOfDate);
});
