<?php

namespace Shm\ShmChat\ShmChatBlueprints;

use Shm\Shm;
use Shm\ShmAuth\Auth;
use Shm\ShmChat\SchemaChat\ShmChannelMembers;
use Shm\ShmChat\SchemaChat\ShmChannels;
use Shm\ShmDB\mDB;
use Shm\ShmRPC\ShmRPC;
use Shm\ShmUtils\I18n;

class ShmChatBlueprintsBaseRPC
{



    public static function accessValidator($channelId)
    {

        Auth::authenticateOrThrow();

        $channel = ShmChannels::findOne([
            '_id' => mDB::id($channelId),
            'members.owner' => Auth::getAuthID(),
            'members.ownerCollection' => Auth::getAuthCollection(),
        ]);





        if (!$channel) {


            if (ShmChannels::hasJoinablePipelineHandler()) {

                $pipeline = ShmChannels::getJoinablePipelineHandler();
                $pipeline[] = [
                    '$match' => [
                        '_id' => mDB::id($channelId),
                    ]
                ];

                $result = ShmChannels::structure()->aggregate($pipeline)->toArray();

                if (count($result) > 0) {
                    return true;
                } else {
                    Shm::error(I18n::get('not_channel_member'));
                }
            }


            Shm::error(I18n::get('not_channel_member'));
        }
    }
}