<?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 App\Support\ApiResponse;
use Illuminate\Http\Request;

class PostLandingController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request)
    {
        $categories = Category::where('model_type', 'Post::class')->take(10)->get();
        $postQuery = fn () => Post::published()->forApi()->take(12)->get();

        $features = PostResource::collection(
            Post::published()->forApi()->where('is_featured', true)->take(8)->get()
        );
        $posts = PostResource::collection($postQuery());
        $tuts = PostResource::collection($postQuery());
        $tech = PostResource::collection($postQuery());
        $public = PostResource::collection($postQuery());
        $news = PostResource::collection($postQuery());

        return ApiResponse::success(compact('categories', 'features', 'posts', 'tech', 'tuts', 'public', 'news'));
    }
}
