<?php

namespace Shm\ShmAdmin\Utils;

use Shm\Shm;
use Shm\ShmAdmin\SchemaCollections\SubAccountRoles;
use Shm\ShmAuth\Auth;
use Shm\ShmTypes\StructureType;

class SubAccountUtils
{



    private static $currentRole = null;

    private static function initRole()
    {

        if (self::$currentRole) {
            return;
        }

        $subAccount = Auth::$subAccount;

        if (!$subAccount) {
            Shm::unauthorizedError();
        }

        $role = $subAccount['role'] ?? null;
        if (!$role) {
            Shm::unauthorizedError();
        }

        $subAccountActive = $subAccount['active'] ?? false;
        if (!$subAccountActive) {
            Shm::unauthorizedError();
        }



        if (!self::$currentRole) {
            self::$currentRole = SubAccountRoles::_collection()->findOne([
                '_id' => $subAccount['role'] ?? null
            ]);
        }

        if (!self::$currentRole) {
            Shm::unauthorizedError();
        }

        $active = self::$currentRole['active'] ?? false;

        if (!$active) {
            Shm::unauthorizedError();
        }
    }

    public static function canShowAction(string $collection, string $actionKey): bool
    {
        self::initRole();


        return self::$currentRole[$collection]['actions'][$actionKey] ?? false;
    }

    public static function updateSchema(StructureType $structure): StructureType
    {


        $collection = $structure->collection;


        self::initRole();

        $collectionItem =  self::$currentRole[$collection] ?? null;



        $canView = $collectionItem['canView'] ?? false;
        $canCreate = $collectionItem['canCreate'] ?? false;
        $canUpdate = $collectionItem['canUpdate'] ?? false;
        $canDelete = $collectionItem['canDelete'] ?? false;
        $canExport = $collectionItem['canExport'] ?? false;
        $canViz = $collectionItem['canViz'] ?? false;


        if (!$canView) {


            $structure->canCreate(false)->canUpdate(false)->canDelete(false);
        } else {

            if (!$canViz) {
                $structure->removeFieldsByType(Shm::viz());
            }

            if (!$canExport) {
                $structure->canExport(false);
            }

            if ($structure->canCreateHasHandler() && !$canCreate) {
                $structure->canCreateRemoveHandler();
            }
            if ($structure->canUpdateHasHandler() && !$canUpdate) {
                $structure->canUpdateRemoveHandler();
            }
            if ($structure->canDeleteHasHandler() && !$canDelete) {
                $structure->canDeleteRemoveHandler();
            }
        }




        return $structure;
    }

    public static function removeNotViewItemsInSchema(StructureType $structure): StructureType
    {





        self::initRole();





        foreach ($structure->items as $key =>  $item) {
            if ($item->type == "adminGroup" || $item->type == 'admin') {
                $structure->items[$key] = self::removeNotViewItemsInSchema($item);
                continue;
            }

            if ($item->type == 'structure' && $item instanceof StructureType) {


                $collection = $item->collection;


                $collectionItem = self::$currentRole[$collection] ?? null;





                $canView = $collectionItem['canView'] ?? false;

                if (!$canView) {
                    unset($structure->items[$key]);
                    continue;
                }
            }
        }


        return $structure;
    }
}
