<?php
declare(strict_types=1);
require_once __DIR__ . '/../app/controllers/ShopController.php';
require_once __DIR__ . '/../app/config/bootstrap.php';
header('Content-Type: application/json; charset=utf-8');
$q = trim((string)($_GET['q'] ?? ''));
if ($q === '') {
    echo json_encode([]);
    exit;
}
try {
    $items = (new ShopController())->search($q);
    echo json_encode($items, JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode([
        'error' => true,
        'message' => 'خطا در جستجو',
    ], JSON_UNESCAPED_UNICODE);
}
Performance::finish('public.search');
