<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Suggestion;
use Illuminate\Http\Request;

class SuggestionController extends Controller
{
    public function index(Request $request)
    {
        $query = Suggestion::query()->with('user');

        if ($request->filled('q')) {
            $q = $request->q;
            $query->where(function ($qry) use ($q) {
                $qry->where('text', 'like', "%{$q}%")
                    ->orWhereHas('user', fn ($u) => $u->where('mobile', 'like', "%{$q}%"));
            });
        }

        $perPage = min(max((int) $request->input('per_page', 15), 10), 100);
        $suggestions = $query->paginate($perPage)->withQueryString();

        return view('admin.suggestions.index', compact('suggestions'));
    }
}
