<?php

use QUI\MasterLogin\Audit;
use QUI\MasterLogin\Manager;
use QUI\Utils\Grid;
use QUI\Utils\Security\Orthos;

QUI::getAjax()->registerFunction(
    'package_quiqqer_masterlogin_ajax_backend_getAuditEntries',
    function (string $searchParams): array {
        $decodedParams = json_decode($searchParams, true);
        $searchParams = is_array($decodedParams) ? Orthos::clearArray($decodedParams) : [];
        $Grid = new Grid($searchParams);
        $entries = Audit::search($searchParams);
        $count = Audit::search([...$searchParams, 'count' => true]);

        return $Grid->parseResult(is_array($entries) ? $entries : [], is_int($count) ? $count : 0);
    },
    ['searchParams'],
    ['Permission::checkAdminUser', Manager::PERMISSION_VIEW_AUDIT]
);
