<?php
namespace App\Http\Controllers\Api;

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

class SearchController extends Controller
{
    // GET /api/search?q=...
    public function __invoke(Request $request)
    {
        $q = $request->validate(['q' => 'required|string|min:2|max:100'])['q'];

        $posts = Post::published()
            ->with(['country', 'category'])
            ->where(fn($query) => $query
                ->whereFullText(['title', 'excerpt'], $q)
                ->orWhere('title', 'like', "%{$q}%")
            )
            ->orderByDesc('published_at')
            ->paginate(12);

        return response()->json($posts);
    }
}
