<?php

declare(strict_types=1);

final class SearchController
{
    public static function index(): void
    {
        $q = mb_substr(trim((string) ($_GET['q'] ?? '')), 0, 120);
        render('search.php', [
            'title' => t('search.title'),
            'searchQuery' => $q,
            'searchResults' => GlobalSearchService::search($q, 20),
        ]);
    }
    public static function json(): void
    {
        $q = mb_substr(trim((string) ($_GET['q'] ?? '')), 0, 120);
        header('Content-Type: application/json; charset=utf-8');
        header('Cache-Control: private, no-store');
        echo json_encode(
            ['query' => $q, 'groups' => GlobalSearchService::search($q)],
            JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR,
        );
    }
}
