<?php

namespace LaraBB\Forum\Actions;

use LaraBB\Forum\UI\Web\Requests\Show;
use LaraBB\Thread\Models\Thread;

/**
 * Class IndexAction
 * @package LaraBB\Forum\Actions
 */
class IndexAction
{
    public function __construct()
    {

    }

    /**
     * @param Show $request
     * @return array
     */
    public function run(Show $request): array
    {
        $request->forum->threads = $request->forum->threads->filter(function(Thread $thread) use($request) {
            if($thread->groups->isEmpty() && $thread->users->isEmpty()) {
                return true;
            }

            return $thread->groups->min('priority') <= $request->user()->groups->max('priority') || $thread->users->contains('uuid', $request->user()->uuid);
        });

        return [
            'forum' => $request->forum
        ];
    }
}
