<?php

namespace LaraBB\Category\Traits;

use Illuminate\Database\Eloquent\Collection;
use LaraBB\Category\Models\Category;
use LaraBB\Category\UI\Web\Requests\Index;
use LaraBB\Category\UI\Web\Requests\Show;
use LaraBB\Forum\Models\Forum;

/**
 *
 */
trait ForumsTrait
{
    /**
     * @param Category $category
     * @param Index|Show $request
     * @return Collection
     */
    public function getForums(Category $category, Index|Show $request): Collection
    {
        return $category->forums->filter(function (Forum $forum) use ($request) {
            if (!isset($request->user()->forumPermissions[$forum->uuid])) {
                return false;
            }

            if ($request->user()->forumPermissions[$forum->uuid]->show_forum) {
                return $forum->users->isEmpty() || $forum->users->contains('uuid', $request->user()->uuid) || $request->user()->isInGroup(['Administrator', 'Moderator']);
            }

            return false;
        });
    }
}
