<?php

declare(strict_types=1);

namespace App\Modules\Inventory\Presentation\Controllers;

use App\Core\Auth;
use App\Core\Request;
use App\Modules\Inventory\Application\Services\ItemLookupService;

final class ItemAjaxController
{
    public function __construct(private ?ItemLookupService $service = null)
    {
        $this->service ??= new ItemLookupService(db());
    }

    public function search(): void
    {
        Auth::requireLogin();
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($this->service->search(trim((string) Request::get('q', ''))), JSON_UNESCAPED_UNICODE);
    }

    public function info(): void
    {
        Auth::requireLogin();
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($this->service->info((int) Request::get('id', 0)), JSON_UNESCAPED_UNICODE);
    }
}
