<?php
namespace App\Models\Usuarios;

class UserGroupModel extends \App\Models\GoBaseModel
{
    protected $table = "auth_user_group";

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

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

    const SORTABLE = [
        1 => "t1.id_group",
        2 => "t1.title",
        3 => "t1.dashboard",
        4 => "t1.rules",
        5 => "t1.token",
        6 => "t1.created_at",
        7 => "t1.updated_at",
    ];

    protected $allowedFields = ["id_group", "title", "dashboard", "rules", "token"];
    protected $returnType = "App\Entities\Usuarios\UserGroupEntity";

    public static $labelField = "title";

    protected $validationRules = [
        "dashboard" => [
            "label" => "UserGroups.dashboard",
            "rules" => "required|max_length[50]",
        ],
        "title" => [
            "label" => "UserGroups.title",
            "rules" => "required|max_length[150]",
        ],
        
    ];

    protected $validationMessages = [
        "dashboard" => [
            "max_length" => "UserGroups.validation.dashboard.max_length",
            "required" => "UserGroups.validation.dashboard.required",
        ],
        "title" => [
            "max_length" => "UserGroups.validation.title.max_length",
            "required" => "UserGroups.validation.title.required",
        ],
        
    ];

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id_group AS id_group, t1.title AS title, t1.dashboard AS dashboard, t1.rules AS rules, t1.token AS token, t1.created_at AS created_at, t1.updated_at AS updated_at"
            );

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id_group", $search)
                ->orLike("t1.title", $search)
                ->orLike("t1.dashboard", $search)
                ->orLike("t1.rules", $search)
                ->orLike("t1.token", $search)
                ->orLike("t1.created_at", $search)
                ->orLike("t1.updated_at", $search)
                ->orLike("t1.id_group", $search)
                ->orLike("t1.title", $search)
                ->orLike("t1.dashboard", $search)
                ->orLike("t1.rules", $search)
                ->orLike("t1.token", $search)
                ->orLike("t1.created_at", $search)
                ->orLike("t1.updated_at", $search)
                ->groupEnd();
    }
}
