<?php

namespace LaraBB\Category\Actions;

use JetBrains\PhpStorm\ArrayShape;
use LaraBB\Category\Tasks\FindTask as FindCategoryTask;
use LaraBB\Category\Traits\ForumsTrait;
use LaraBB\Category\UI\Web\Requests\Show;
use LaraBB\Forum\Models\Forum;

/**
 * Class CategoryAction
 * @package LaraBB\Category\Actions
 */
class ShowAction
{
    use ForumsTrait;

    /**
     * CategoryAction constructor.
     * @param FindCategoryTask $findCategoryTask
     */
    public function __construct(private readonly FindCategoryTask $findCategoryTask)
    {
    }

    /**
     * @param Show $request
     * @return array
     */
    #[ArrayShape([
        'category' => "\LaraBB\Category\Models\Category"
    ])]
    public function run(Show $request): array
    {
        $category = $this->findCategoryTask->bySlug($request->route('slug'), [
            'forums' => function ($query) {
                return $query->with([
                    'users',
                    'lastpost' => function ($query) {
                        return $query->with(['thread', 'createdBy']);
                    }
                ]);
            }
        ]);

        $category->forums = $this->getForums($category, $request);

        return [
            'category' => $category
        ];
    }
}
