<?php

namespace App\Http\Controllers;

use App\Services\SearchService;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:search.view');
    }

    public function index(Request $request, SearchService $search)
    {
        $q = (string) $request->get('q', '');
        $results = $q !== '' ? $search->search($q) : [];

        if ($request->wantsJson()) {
            return response()->json($results);
        }

        return view('search.index', compact('q', 'results'));
    }
}
