<?php
namespace App\Models\Configuracion;

class ProvinciaModel extends \App\Models\BaseModel
{
    protected $table = "lg_provincias";

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

    const SORTABLE = [
        1 => "t1.id",
        2 => "t1.code",
        3 => "t1.nombre",
        4 => "t1.pais_id",
        5 => "t1.created_at",
        6 => "t1.updated_at",
        7 => "t2.nombre",
    ];

    protected $allowedFields = ["code", "nombre", "pais_id"];
    protected $returnType = "App\Entities\Configuracion\ProvinciaEntity";

    public static $labelField = "nombre";

    protected $validationRules = [
        "code" => [
            "label" => "Provincias.code",
            "rules" => "trim|required|max_length[2]|is_unique[lg_provincias.code,id,{id}]",
        ],
        "nombre" => [
            "label" => "Provincias.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
    ];

    protected $validationMessages = [
        "code" => [
            "is_unique" => "Provincias.validation.code.is_unique",
            "max_length" => "Provincias.validation.code.max_length",
            "required" => "Provincias.validation.code.required",
        ],
        "nombre" => [
            "max_length" => "Provincias.validation.nombre.max_length",
            "required" => "Provincias.validation.nombre.required",
        ],
    ];

    public function findAllWithPaises(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.nombre AS pais FROM " .
            $this->table .
            " t1  LEFT JOIN lg_paises t2 ON t1.pais_id = t2.id";
        if (!is_null($limit) && intval($limit) > 0) {
            $sql .= " LIMIT " . $limit;
        }

        if (!is_null($offset) && intval($offset) > 0) {
            $sql .= " OFFSET " . $offset;
        }

        $query = $this->db->query($sql);
        $result = $query->getResultObject();
        return $result;
    }

    /**
     * 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 AS id, t1.code AS code, t1.nombre AS nombre, t1.created_at AS created_at, t1.updated_at AS updated_at, t2.nombre AS pais"
            );
        $builder->join("lg_paises t2", "t1.pais_id = t2.id", "left");

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t1.code", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.created_at", $search)
                ->orLike("t1.updated_at", $search)
                ->orLike("t2.id", $search)
                ->orLike("t1.id", $search)
                ->orLike("t1.code", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.pais_id", $search)
                ->orLike("t1.created_at", $search)
                ->orLike("t1.updated_at", $search)
                ->orLike("t2.nombre", $search)
                ->groupEnd();
    }
}
