<?php

declare(strict_types=1);

namespace App\Http\Controllers\Post;

use App\Http\Controllers\Controller;
use App\Http\Resources\PostResource;
use App\Models\Category;
use App\Models\Post;
use Illuminate\Http\Request;

class PostCategoryController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request, Category $category)
    {
        return PostResource::collection(
            Post::published()
                ->forApi()
                ->whereHas('categories', function ($query) use ($category): void {
                    $query->where('category_id', $category->id);
                })
                ->paginate()
        );
    }
}
