<?php
    declare(strict_types=1);

    namespace Icing\Model\Table;

    use Cake\Datasource\EntityInterface;
    use Cake\Datasource\ResultSetInterface;
    use Cake\Validation\Validator;
    use Closure;
    use Icing\Model\Entity\Role;
    use Psr\SimpleCache\CacheInterface;

    /**
     * Roles Model
     *
     * @property \Icing\Model\Table\PermissionsTable&\Cake\ORM\Association\BelongsToMany $Permissions
     * @property \Icing\Model\Table\UserGroupsTable&\Cake\ORM\Association\BelongsToMany $UserGroups
     *
     * @method Role newEmptyEntity()
     * @method Role newEntity(array $data, array $options = [])
     * @method array<Role> newEntities(array $data, array $options = [])
     * @method Role get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
     * @method Role findOrCreate($search, ?callable $callback = null, array $options = [])
     * @method Role patchEntity(EntityInterface $entity, array $data, array $options = [])
     * @method array<Role> patchEntities(iterable $entities, array $data, array $options = [])
     * @method Role|false save(EntityInterface $entity, array $options = [])
     * @method Role saveOrFail(EntityInterface $entity, array $options = [])
     * @method iterable<Role>|ResultSetInterface<Role>|false saveMany(iterable $entities, array $options = [])
     * @method iterable<Role>|ResultSetInterface<Role> saveManyOrFail(iterable $entities, array $options = [])
     * @method iterable<Role>|ResultSetInterface<Role>|false deleteMany(iterable $entities, array $options = [])
     * @method iterable<Role>|ResultSetInterface<Role> deleteManyOrFail(iterable $entities, array $options = [])
     */
    class RolesTable extends Table
    {
        /**
         * Initialize method
         *
         * @param array $config The configuration for the Table.
         *
         * @return void
         */
        public function initialize(array $config): void
        {
            parent::initialize($config);

            $this->setTable('ws_icing_roles');
            $this->setDisplayField('name');
            $this->setPrimaryKey('id');

            $this->belongsToMany(
                'Icing.Permissions',
                [
                    'foreignKey'       => 'role_id',
                    'targetForeignKey' => 'permission_id',
                    'joinTable'        => 'ws_icing_roles_permissions'
                ]
            );
            $this->belongsToMany(
                'Icing.UserGroups',
                [
                    'foreignKey'       => 'role_id',
                    'targetForeignKey' => 'user_group_id',
                    'joinTable'        => 'ws_icing_user_groups_roles',
                ]
            );
        }

        /**
         * Default validation rules.
         *
         * @param \Cake\Validation\Validator $validator Validator instance.
         *
         * @return \Cake\Validation\Validator
         */
        public function validationDefault(Validator $validator): Validator
        {
            $validator
                ->scalar('name')
                ->maxLength('name', 255)
                ->allowEmptyString('name');

            $validator
                ->scalar('description')
                ->allowEmptyString('description');

            return $validator;
        }
    }
