<?php

namespace App\Models\Clientes;

class ClienteContactoModel extends \App\Models\BaseModel
{
    protected $table = "cliente_contactos";

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

    const SORTABLE = [
        0 => "t1.nombre",
        1 => "t1.apellidos",
        2 => "t1.cargo",
        3 => "t1.telefono",
        4 => "t1.email",
    ];

    protected $allowedFields = ["cliente_id", "cargo", "nombre", "apellidos", "telefono", "email"];
    protected $returnType = "App\Entities\Clientes\ClienteContactoEntity";

    protected $useTimestamps = true;
    protected $useSoftDeletes = true;
    protected $deletedField = 'deleted_at';

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "nombre";

    protected $validationRules = [
        "apellidos" => [
            "label" => "ClienteContactos.apellidos",
            "rules" => "trim|max_length[500]",
        ],
        "cargo" => [
            "label" => "ClienteContactos.cargo",
            "rules" => "trim|max_length[100]",
        ],
        "email" => [
            "label" => "ClienteContactos.email",
            "rules" => "trim|max_length[150]|valid_email|permit_empty",
        ],
        "nombre" => [
            "label" => "ClienteContactos.nombre",
            "rules" => "trim|max_length[100]",
        ],
        "telefono" => [
            "label" => "ClienteContactos.telefono",
            "rules" => "trim|max_length[20]",
        ],
    ];

    protected $validationMessages = [
        "apellidos" => [
            "max_length" => "ClienteContactos.validation.apellidos.max_length",
        ],
        "cargo" => [
            "max_length" => "ClienteContactos.validation.cargo.max_length",
        ],
        "email" => [
            "max_length" => "ClienteContactos.validation.email.max_length",
            "valid_email" => "ClienteContactos.validation.email.valid_email",
        ],
        "nombre" => [
            "max_length" => "ClienteContactos.validation.nombre.max_length",
        ],
        "telefono" => [
            "max_length" => "ClienteContactos.validation.telefono.max_length",
        ],
    ];

    public function findAllWithClientes(string $selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.nombre AS cliente_id FROM " .
            $this->table .
            " t1  LEFT JOIN clientes t2 ON t1.cliente_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 = "", $cliente_id = -1)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.cargo AS cargo, t1.nombre AS nombre, t1.apellidos AS apellidos, t1.telefono AS telefono, t1.email AS email, t1.cliente_id AS cliente_id"
            );

        $builder->where('t1.cliente_id', $cliente_id);
        $builder->where("t1.deleted_at", null);
        $builder->join("clientes t2", "t1.cliente_id = t2.id", "left");

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.cargo", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.apellidos", $search)
                ->orLike("t1.telefono", $search)
                ->orLike("t1.email", $search)
                ->groupEnd();
    }
}
