<?php


namespace Shm\ShmAdmin\SchemaCollections;

use Shm\Collection\Collection;
use Shm\Shm;
use Shm\ShmAuth\Auth;

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

class SubAccounts extends Collection
{


    public $isShmCollection = true;

    public $collection = 'subAccounts';



    public function schema(): StructureType
    {
        $schema = Shm::structure([

            '_id' => Shm::ID(),
            'created_at' => Shm::timestamp(),
            'updated_at' => Shm::timestamp(),

            'photo' => Shm::fileIDImage()->title('Фото')->inAdmin()->editable()->inTable(),
            'active' => Shm::bool()->title('Разрешить доступ')->default(true)->inAdmin(!Auth::subAccountAuth())->editable()->inTable(),
            'name' => Shm::string()->title('Имя')->setCol(12)->inAdmin()->editable()->inTable(),
            'surname' => Shm::string()->title('Фамилия')->setCol(12)->inAdmin()->editable()->inTable(),
            'login' => Shm::login()->title('Логин')->setCol(12)->inAdmin()->editable()->inTable()->required(),
            'password' => Shm::password()->title('Пароль')->setCol(12)->inAdmin()->editable(),
            // 'last_used' => Shm::timestamp()->title('Последняя активность')->inAdmin(!Auth::subAccountAuth())->inTable(),

            'role' => SubAccountRoles::ID()->title('Роль')->inAdmin()->editable(),

        ]);


        $schema->title("Доступы");

        $schema->icon(LuIcons::UserLock());

        $schema->canCreate()->canDelete()->canUpdate();



        if (Auth::isAuthenticated()) {

            $schema->insertValues([

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


            $schema->pipeline([

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

            ]);
        }


        return   $schema;
    }
}
