<?php
namespace App\Models\Usuarios;

class UserModel extends \App\Models\GoBaseModel
{
    protected $table = "auth_user";

    /**
     * Whether primary key uses auto increment.
     *
     * @var bool
     */
    protected $useAutoIncrement = true;
    protected $useTimestamps = true;
    protected $dateFormat = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    protected $primaryKey = "id_user";

    protected $allowedFields = [
        "group",
        "first_name",
        "last_name",
        "date_birth",
        "address",
        "city",
        "state",
        "country",
        "zip_code",
        "mobile",
        "email",
        "password",
        "last_ip",
        "last_access",
        "picture",
        "language",
        "blocked",
        "email_confirmed",
        "status",
        "token",
    ];
    protected $returnType = "App\Entities\Usuarios\UserEntity";

    public static $labelField = "first_name";

    
    protected $validationRules = [
        "address" => [
            "label" => "Users.address",
            "rules" => "trim|max_length[255]",
        ],
        "blocked" => [
            "label" => "Users.blocked",
            "rules" => "required|permit_empty",
        ],
        "city" => [
            "label" => "Users.city",
            "rules" => "trim|max_length[255]",
        ],
        "country" => [
            "label" => "Users.country",
            "rules" => "trim|required|max_length[2]",
        ],
        "date_birth" => [
            "label" => "Users.dateBirth",
            "rules" => "valid_date|permit_empty",
        ],
        "email" => [
            "label" => "Users.email",
            "rules" => "trim|required|max_length[150]|valid_email",
        ],
        "email_confirmed" => [
            "label" => "Users.emailConfirmed",
            "rules" => "required|integer",
        ],
        "first_name" => [
            "label" => "Users.firstName",
            "rules" => "trim|required|max_length[150]",
        ],
        "language" => [
            "label" => "Users.language",
            "rules" => "trim|required|max_length[10]",
        ],/*
        "last_access" => [
            "label" => "Users.lastAccess",
            "rules" => "valid_date",
        ],*/
        "last_ip" => [
            "label" => "Users.lastIp",
            "rules" => "max_length[50]",
        ],
        "last_name" => [
            "label" => "Users.lastName",
            "rules" => "trim|required|max_length[150]",
        ],
        "mobile" => [
            "label" => "Users.mobile",
            "rules" => "trim|required|max_length[50]",
        ],
        "password" => [
            "label" => "Users.password",
            "rules" => "required|max_length[35]",
        ],
        "picture" => [
            "label" => "Users.picture",
            "rules" => "trim|required|max_length[150]",
        ],
        "state" => [
            "label" => "Users.state",
            "rules" => "trim|max_length[255]",
        ],
        "zip_code" => [
            "label" => "Users.zipCode",
            "rules" => "trim|max_length[50]",
        ],
    ];

    protected $validationMessages = [
        "address" => [
            "max_length" => "Users.validation.address.max_length",
        ],
        
        "blocked" => [
            "required" => "Users.validation.email_confirmed.required",
        ],
        "city" => [
            "max_length" => "Users.validation.city.max_length",
        ],
        "country" => [
            "max_length" => "Users.validation.country.max_length",
            "required" => "Users.validation.country.required",
        ],
        "date_birth" => [
            "valid_date" => "Users.validation.date_birth.valid_date",
        ],
        "email" => [
            "max_length" => "Users.validation.email.max_length",
            "required" => "Users.validation.email.required",
            "valid_email" => "Users.validation.email.valid_email",
        ],
        "email_confirmed" => [
            "integer" => "Users.validation.email_confirmed.integer",
            "required" => "Users.validation.email_confirmed.required",
        ],
        "first_name" => [
            "max_length" => "Users.validation.first_name.max_length",
            "required" => "Users.validation.first_name.required",
        ],
        
        "language" => [
            "max_length" => "Users.validation.language.max_length",
            "required" => "Users.validation.language.required",
        ],
        "last_access" => [
            "required" => "Users.validation.last_access.required",
            "valid_date" => "Users.validation.last_access.valid_date",
        ],
        "last_ip" => [
            "max_length" => "Users.validation.last_ip.max_length",
            "required" => "Users.validation.last_ip.required",
        ],
        "last_name" => [
            "max_length" => "Users.validation.last_name.max_length",
            "required" => "Users.validation.last_name.required",
        ],
        "mobile" => [
            "max_length" => "Users.validation.mobile.max_length",
            "required" => "Users.validation.mobile.required",
        ],
        "password" => [
            "max_length" => "Users.validation.password.max_length",
            "required" => "Users.validation.password.required",
        ],
        "picture" => [
            "max_length" => "Users.validation.picture.max_length",
            "required" => "Users.validation.picture.required",
        ],
        "state" => [
            "max_length" => "Users.validation.state.max_length",
            "required" => "Users.validation.state.required",
        ],
        "zip_code" => [
            "max_length" => "Users.validation.zip_code.max_length",
        ],
    ];

    public function getGroupsTitles($user_token){
        
        $sql = 'SELECT `auth_user_group`.`title`   FROM `auth_user_group` 
        JOIN `group_user` ON `auth_user_group`.`token` = `group_user`.`token_group`
        JOIN `auth_user` ON `auth_user`.`token` = `group_user`.`token_user`
        WHERE `auth_user`.`token` = \''. $user_token . '\'';
        

        $query = $this->db->query($sql);
        $result = $query->getResultObject();
        $data = [];
        foreach($result as $r){
            array_push($data, $r->title);
        }
        return implode(',', $data);
        
        
        
    }
}
