<?php

namespace App\Modules\Platform\Lookup\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Platform\Lookup\Requests\LookupSearchRequest;
use App\Modules\Platform\Lookup\Services\LookupSearchService;
use Illuminate\Http\JsonResponse;

class LookupSearchController extends Controller
{
    public function __invoke(LookupSearchRequest $request, LookupSearchService $service): JsonResponse
    {
        $validated = $request->validated();

        return response()->json([
            'results' => $service->search(
                (string) $validated['type'],
                (string) ($validated['q'] ?? ''),
                (int) ($validated['limit'] ?? 30),
                isset($validated['selected']) ? (int) $validated['selected'] : null,
                isset($validated['exclude']) ? (int) $validated['exclude'] : null,
                isset($validated['parent']) ? (int) $validated['parent'] : null,
            ),
        ]);
    }
}
