<?php
namespace App\Models\Usuarios;

class UserModel extends \App\Models\BaseModel
{
    protected $table = "users";

    /**
     * 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";

    protected $allowedFields = [
        "username",
        "first_name",
        "last_name",
        "client_id",
        "status",
        "status_message",
        "active",
        "comments",
        "last_active",
        "created_at",
        "updated_at",
        "deleted_at",
    ];
    protected $returnType = "App\Entities\Usuarios\UserEntity";

    public static $labelField = "first_name";

    
    protected $validationRules = [
        "first_name" => [
            "label" => "Users.firstName",
            "rules" => "trim|max_length[150]",
        ],
        "last_name" => [
            "label" => "Users.lastName",
            "rules" => "trim|max_length[150]",
        ],
    ];

    protected $validationMessages = [
        "first_name" => [
            "max_length" => "Users.validation.first_name.max_length",
            "required" => "Users.validation.first_name.required",
        ],
        "last_name" => [
            "max_length" => "Users.validation.last_name.max_length",
            "required" => "Users.validation.last_name.required",
        ], 
    ];

    public function getGroupsTitles($user_token){
        
        $sql = 'SELECT `auth_groups`.`title`   FROM `auth_groups` 
        JOIN `group_user` ON `auth_groups`.`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);
        
        
        
    }
}
