<?php

namespace App\Modules\Navigation\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Navigation\Services\NavigationSearchService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NavigationSearchController extends Controller
{
    public function __invoke(Request $request, NavigationSearchService $search): JsonResponse
    {
        $validated = $request->validate([
            'q' => ['nullable', 'string', 'max:120'],
            'group' => ['nullable', 'string', 'max:64'],
            'limit' => ['nullable', 'integer', 'min:5', 'max:50'],
        ]);

        $results = $search->search(
            $request->user(),
            (string) ($validated['q'] ?? ''),
            (int) ($validated['limit'] ?? 25),
            isset($validated['group']) ? (string) $validated['group'] : null,
        );

        return response()->json(['data' => $results]);
    }
}
