<?php


namespace Shm\ShmAdmin\SchemaCollections;

use Shm\Collection\Collection;
use Shm\Shm;
use Shm\ShmAdmin\AdminPanel;
use Shm\ShmAuth\Auth;

use Shm\ShmTypes\StructureType;
use Shm\ShmUtils\LuIcons;

class SubAccountRoles extends Collection
{


    public $isShmCollection = true;
    public $collection = 'subAccountRoles';


    public function schema(): StructureType
    {

        $_items = $this->getItems(AdminPanel::$schema);

        $structureItems = [

            'title' => Shm::string()->title("Название")->inAdmin()->editable()->required()->col(12)->display()->inTable(),
            "active" => Shm::bool()->title("Доступ разрешен")->inAdmin()->editable()->default(true)->col(12)->inTable(),

            ...$_items

        ];



        //AdminPanel

        $schema = Shm::structure($structureItems);

        $schema->title("Роли");
        $schema->icon(LuIcons::FileUser());
        $schema->canCreate()->canDelete()->canUpdate();



        $schema->insertValues([

            'owner' => Auth::getAuthID(),
            'collection' => Auth::getAuthCollection(),
        ]);


        $schema->pipeline([

            [
                '$match' => [
                    'owner' => Auth::getAuthID(),
                    'collection' => Auth::getAuthCollection(),
                ]
            ],

        ]);

        return $schema;
    }


    private function getItems(StructureType $structure): array
    {

        $result = [];





        foreach ($structure->items as $item) {
            if ($item->type == "adminGroup" || $item->type == 'admin') {
                $result = [...$result, ...$this->getItems($item)];
            }


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

                $isSingle = $item->single;


                $_items = [
                    'canView' => Shm::bool()->title("Просмотр")->inAdmin()->editable()->default(false)->setCol(4),


                    'canCreate' => Shm::bool()->title("Создание")->inAdmin()->editable(!$isSingle && $item->canCreateHasHandler())->default(false)->setCol(4),

                    'canUpdate' => Shm::bool()->title("Редактирование")->inAdmin()->editable($item->canUpdateHasHandler())->default(false)->setCol(4),
                    'canDelete' => Shm::bool()->title("Удаление")->inAdmin()->editable(!$isSingle && $item->canDeleteHasHandler())->default(false)->setCol(4),
                    'canExport' => Shm::bool()->title("Экспорт")->inAdmin()->editable(!$isSingle)->default(false)->setCol(4),
                    'canViz' => Shm::bool()->title("Аналитика")->inAdmin()->editable(!$isSingle)->default(false)->setCol(4),
                ];

                $actions = $item->getActions();

                $itemsActions = [];
                foreach ($actions as $action) {
                    $itemsActions[$action->key] = Shm::bool()->title($action->title())->inAdmin()->editable()->default(false)->setCol(6);
                }

                if (count($itemsActions) > 0) {
                    $_items['actions'] = Shm::structure($itemsActions)->title("Действия")->inAdmin()->editable();
                }

                $result = [
                    ...$result,


                    $item->collection => Shm::structure([


                        ...$_items

                    ])->title($item->title)->inAdmin()->editable(),




                ];
            }
        }


        return $result;
    }
}
