<?php

namespace App\Models\Usuarios;

class PermisosModel extends \App\Models\BaseModel
{
    protected $table = "auth_permissions";

    /**
     * Whether primary key uses auto increment.
     *
     * @var bool
     */
    protected $useAutoIncrement = true;

    protected $primaryKey = "id";

    protected $useTimestamps = true;
    protected $dateFormat = 'datetime';
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';

    const SORTABLE = [
        1 => "t1.id",
        2 => "t1.keyword",
        3 => "t1.name",
        4 => "t1.description",
        5 => "t1.rules",
        6 => "t1.created_at",
        7 => "t1.updated_at",
    ];

    protected $allowedFields = ["id", "keyword", "name", "description", "rules"];
    protected $returnType = "App\Entities\Usuarios\PermisosEntity";

    public static $labelField = "name";

    protected $validationRules = [
        "description" => [
            "label" => "UserGroups.description",
            "rules" => "max_length[150]",
        ],
        "name" => [
            "label" => "UserGroups.name",
            "rules" => "required|max_length[50]",
        ],

    ];

    protected $validationMessages = [
        "description" => [
            "max_length" => "UserGroups.validation.description.max_length",
            "required" => "UserGroups.validation.description.required",
        ],
        "name" => [
            "max_length" => "UserGroups.validation.name.max_length",
            "required" => "UserGroups.validation.name.required",
        ],

    ];


    public function getPermissionsList()
    {
        $allRecords = $this->findAll();
        $result = [];
        foreach ($allRecords as $record) {
            foreach ($record->rules as $section => $permissions) {
                foreach ($permissions as $permission) {
                    $result[$section . "." . $permission] = "Can " . $permission;
                }
                // Add menu visibility permission (automatic management)
                $result[$section . ".menu"] = "Menu shall be visualize";
            }
        }
        return $result;
    }


}
