<?php

use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\I18n;
use MintyPHP\Service\Search\SearchDataService;
use MintyPHP\Support\Guard;

$session = app(SessionStoreInterface::class)->all();
Guard::requireLogin();
gridRequireGetRequest();

$userId = (int) ($session['user']['id'] ?? 0);
$filters = gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php');

if ($filters['search'] === '') {
    gridJsonDataResult([], 0);
}

$locale = I18n::$locale ?? (I18n::$defaultLocale ?? 'de');
$result = app(SearchDataService::class)->buildResultData(
    $userId,
    $filters['search'],
    $locale,
    $filters['limit'],
    $filters['offset']
);

gridJsonDataResult(
    (array) ($result['data'] ?? []),
    (int) ($result['total'] ?? 0)
);
