<?php

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

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

$filterSchema = require __DIR__ . '/filter-schema.php';
$filters = gridParseFilters(requestInput()->queryAll(), gridSchemaQuery($filterSchema));
if ($filters['q'] === '') {
    Router::json([]);
}

$userId = (int) ($session['user']['id'] ?? 0);
$locale = I18n::$locale ?? (I18n::$defaultLocale ?? 'de');
$result = app(SearchDataService::class)->buildPreviewData($userId, $filters['q'], $locale, 5);

Router::json($result);
