<?php\n\nrequire __DIR__ . \'/../vendor/autoload.php\';\nrequire __DIR__ . \'/../src/Database.php\';\nrequire __DIR__ . \'/../src/Models/Item.php\';\nrequire __DIR__ . \'/../src/Models/Category.php\';\nrequire __DIR__ . \'/../src/Services/TranslationService.php\';\n\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Slim\\Psr7\\Factory\\ResponseFactory;\nuse Slim\\Psr7\\Factory\\ServerRequestFactory;\n\n// --- Setup ---\n$db = App\\Database\\Database::getInstance();\n$translator = new App\\Services\\TranslationService();\n\n// --- Handle Search Action ---\n// This endpoint is specifically for AJAX search requests\nif (($_SERVER['REQUEST_METHOD'] === 'GET') && isset($_GET['action']) && $_GET['action'] === 'search_items') { \n    $locale = $translator->getCurrentLocale(); // Get current locale\n    if (isset($_GET['locale'])) {\n        $requestedLocale = filter_input(INPUT_GET, 'locale', FILTER_SANITIZE_STRING);\n        if (in_array($requestedLocale, ['en', 'nl'])) {\n            $locale = $requestedLocale;\n            $translator->setLocale($locale); // Set locale for this request\n        }\n    }\n    \n    $searchTerm = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING);\n    $categoryId = filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);\n\n    $sql = 'SELECT i.id, i.name, i.description, c.name as category_name, i.category_id \n             FROM items i \n             LEFT JOIN categories c ON i.category_id = c.id WHERE 1=1';\n    $params = [];\n\n    if ($searchTerm) {\n        $sql .= ' AND i.name LIKE :searchTerm';\n        $params[':searchTerm'] = '%' . $searchTerm . '%';\n    }\n    if ($categoryId !== false) {\n        $sql .= ' AND i.category_id = :category_id';\n        $params[':category_id'] = $categoryId;\n    }\n    $sql .= ' ORDER BY i.name';\n\n    header('Content-Type: application/json');\n    try {\n        $stmt = $db->prepare($sql);\n        $stmt->execute($params);\n        $items = $stmt->fetchAll(PDO::FETCH_ASSOC);\n        echo json_encode(['success' => true, 'items' => $items]);\n    } catch (PDOException $e) {\n        error_log("Error searching items: " . $e->getMessage());\n        echo json_encode(['success' => false, 'message' => $translator->trans('Error searching items.')]);\n    }\n    exit;\n}\n\n// If this file is accessed directly without the correct action, return a 404 or error.\nheader('HTTP/1.0 404 Not Found');\necho json_encode(['error' => 'This is an API endpoint. Access via AJAX or main router.']);\n\n?>