<?php

namespace App\Models\Clientes;

use App\Entities\Clientes\ClienteEntity;

class ClienteModel extends \App\Models\BaseModel
{
    protected $table = "clientes";

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

    const SORTABLE = [
        0 => "t1.id",
        1 => "t1.nombre",
        2 => "t1.alias",
        3 => "t1.cif",
        4 => "t1.email",
        5 => "t1.comercial_id",
        6 => "t1.forma_pago_id",
        7 => "t1.vencimiento",
    ];

    protected $allowedFields = [
        "nombre",
        "alias",
        "cif",
        "direccion",
        "ciudad",
        "comunidad_autonoma_id",
        "provincia_id",
        "cp",
        "pais_id",
        "telefono",
        "email",
        "comercial_id",
        "soporte_id",
        "forma_pago_id",
        "vencimiento",
        "fecha_vencimiento",
        "margen",
        "descuento",
        "limite_credito",
        "limite_credito_user_id",
        "limite_credito_change_at",
        "credito_asegurado",
        "ccc",
        "ccc_cliente",
        "num_cuenta",
        "message_tracking",
        "message_production_start",
        "tirada_flexible",
        "descuento_tirada_flexible",
        "comentarios_tirada_flexible",
        "margen_plantilla_id",
        "comentarios",
        "user_created_id",
        "user_update_id",
    ];
    protected $returnType = ClienteEntity::class;
    protected $useSoftDeletes   = true;
    protected $deletedField = 'deleted_at';

    public static $labelField = "nombre";

    protected $validationRules = [
        "alias" => [
            "label" => "Clientes.alias",
            "rules" => "trim|required|max_length[255]",
        ],
        "ccc" => [
            "label" => "Clientes.ccc",
            "rules" => "trim|max_length[100]",
        ],
        "ccc_cliente" => [
            "label" => "Clientes.cccCliente",
            "rules" => "trim|max_length[100]",
        ],
        "cif" => [
            "label" => "Clientes.cif",
            "rules" => "trim|required|max_length[50]",
        ],
        "ciudad" => [
            "label" => "Clientes.ciudad",
            "rules" => "trim|max_length[100]",
        ],
        "comentarios" => [
            "label" => "Clientes.comentarios",
            "rules" => "trim|max_length[16313]",
        ],
        "comentarios_tirada_flexible" => [
            "label" => "Clientes.comentariosTiradaFlexible",
            "rules" => "trim|max_length[16313]",
        ],
        "cp" => [
            "label" => "Clientes.cp",
            "rules" => "trim|max_length[10]",
        ],
        "descuento" => [
            "label" => "Clientes.descuento",
            "rules" => "required|decimal",
        ],
        "descuento_tirada_flexible" => [
            "label" => "Clientes.descuentoTiradaFlexible",
            "rules" => "required|decimal",
        ],
        "direccion" => [
            "label" => "Clientes.direccion",
            "rules" => "trim|max_length[300]",
        ],
        "email" => [
            "label" => "Clientes.email",
            "rules" => "trim|max_length[150]|valid_email|permit_empty",
        ],
        "fecha_vencimiento" => [
            "label" => "Clientes.fechaVencimiento",
            "rules" => "trim|max_length[100]",
        ],
        "limite_credito" => [
            "label" => "Clientes.limiteCredito",
            "rules" => "required|decimal",
        ],
        "margen" => [
            "label" => "Clientes.margen",
            "rules" => "required|decimal",
        ],
        "margen_plantilla_id" => [
            "label" => "Clientes.margenPlantillaId",
            "rules" => "integer|permit_empty",
        ],
        "nombre" => [
            "label" => "Clientes.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "num_cuenta" => [
            "label" => "Clientes.numCuenta",
            "rules" => "trim|max_length[10]",
        ],
        "telefono" => [
            "label" => "Clientes.telefono",
            "rules" => "trim|max_length[60]",
        ],
        "vencimiento" => [
            "label" => "Clientes.vencimiento",
            "rules" => "required|integer",
        ],
    ];

    protected $validationMessages = [
        "alias" => [
            "max_length" => "Clientes.validation.alias.max_length",
            "required" => "Clientes.validation.alias.required",
        ],
        "ccc" => [
            "max_length" => "Clientes.validation.ccc.max_length",
        ],
        "ccc_cliente" => [
            "max_length" => "Clientes.validation.ccc_cliente.max_length",
        ],
        "cif" => [
            "max_length" => "Clientes.validation.cif.max_length",
            "required" => "Clientes.validation.cif.required",
        ],
        "ciudad" => [
            "max_length" => "Clientes.validation.ciudad.max_length",
        ],
        "comentarios" => [
            "max_length" => "Clientes.validation.comentarios.max_length",
        ],
        "comentarios_tirada_flexible" => [
            "max_length" => "Clientes.validation.comentarios_tirada_flexible.max_length",
            "required" => "Clientes.validation.comentarios_tirada_flexible.required",
        ],
        "cp" => [
            "max_length" => "Clientes.validation.cp.max_length",
        ],
        "descuento" => [
            "decimal" => "Clientes.validation.descuento.decimal",
            "required" => "Clientes.validation.descuento.required",
        ],
        "descuento_tirada_flexible" => [
            "decimal" => "Clientes.validation.descuento_tirada_flexible.decimal",
            "required" => "Clientes.validation.descuento_tirada_flexible.required",
        ],
        "direccion" => [
            "max_length" => "Clientes.validation.direccion.max_length",
        ],
        "email" => [
            "max_length" => "Clientes.validation.email.max_length",
            "valid_email" => "Clientes.validation.email.valid_email",
        ],
        "fecha_vencimiento" => [
            "max_length" => "Clientes.validation.fecha_vencimiento.max_length",
        ],

        "limite_credito" => [
            "decimal" => "Clientes.validation.limite_credito.decimal",
            "required" => "Clientes.validation.limite_credito.required",
        ],
        "limite_credito_change_at" => [
            "required" => "Clientes.validation.limite_credito_change_at.required",
            "valid_date" => "Clientes.validation.limite_credito_change_at.valid_date",
        ],
        "limite_credito_user_id" => [
            "integer" => "Clientes.validation.limite_credito_user_id.integer",
            "required" => "Clientes.validation.limite_credito_user_id.required",
        ],
        "margen" => [
            "decimal" => "Clientes.validation.margen.decimal",
            "required" => "Clientes.validation.margen.required",
        ],
        "margen_plantilla_id" => [
            "integer" => "Clientes.validation.margen_plantilla_id.integer",
        ],
        "nombre" => [
            "max_length" => "Clientes.validation.nombre.max_length",
            "required" => "Clientes.validation.nombre.required",
        ],
        "num_cuenta" => [
            "max_length" => "Clientes.validation.num_cuenta.max_length",
        ],
        "telefono" => [
            "max_length" => "Clientes.validation.telefono.max_length",
        ],
        "tienda_id" => [
            "integer" => "Clientes.validation.tienda_id.integer",
        ],
        "user_created_id" => [
            "integer" => "Clientes.validation.user_created_id.integer",
            "required" => "Clientes.validation.user_created_id.required",
        ],
        "user_update_id" => [
            "integer" => "Clientes.validation.user_update_id.integer",
            "required" => "Clientes.validation.user_update_id.required",
        ],
        "vencimiento" => [
            "integer" => "Clientes.validation.vencimiento.integer",
            "required" => "Clientes.validation.vencimiento.required",
        ],
    ];
    public function findAllWithAllRelations($selcols = "*", int $limit = null, int $offset = 0)
    {
        $sql =
            "SELECT t1." .
            $selcols .
            ",  t2.nombre AS comunidad_autonoma_id,  t3.nombre AS provincia,  t4.nombre AS pais_id,  t5.first_name AS comercial,  
            t6.last_name AS soporte,  t7.nombre AS forma_pago_id FROM " .
            $this->table .
            " t1  LEFT JOIN lg_comunidades_autonomas t2 ON t1.comunidad_autonoma_id = t2.id 
            LEFT JOIN lg_provincias t3 ON t1.provincia_id = t3.id 
            LEFT JOIN lg_paises t4 ON t1.pais_id = t4.id 
            LEFT JOIN users t5 ON t1.comercial_id = t5.id 
            LEFT JOIN users t6 ON t1.soporte_id = t6.id 
            LEFT JOIN formas_pago t7 ON t1.forma_pago_id = t7.id";
        if (!is_null($limit) && intval($limit) > 0) {
            $sql .= " LIMIT " . intval($limit);
        }

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

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

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource($search = [])
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.nombre AS nombre, t1.alias AS alias, t1.cif AS cif, t1.email AS email, t1.vencimiento AS vencimiento, t5.first_name AS comercial, t7.nombre AS forma_pago_id"
            )
            ->where("t1.deleted_at", null);;
        $builder->join("users t5", "t1.comercial_id = t5.id", "left");
        $builder->join("formas_pago t7", "t1.forma_pago_id = t7.id", "left");

        if (empty($search))
            return $builder;
        else {
            $builder->groupStart();
            foreach ($search as $col_search) {
                $column = self::SORTABLE[$col_search[0]];
                $value = $col_search[2];
                $builder->where("LOWER(CONVERT($column USING utf8)) COLLATE utf8_general_ci LIKE", "%" . strtolower($value) . "%");
            }
            $builder->groupEnd();
            return $builder;
        }
    }

    /*
        TO-DO: Implementar la lógica de negocio para el crédito disponible
    */
    public function creditoDisponible($cliente_id, $total_pedido = 0)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.limite_credito AS limite_credito"
            )
            ->where("t1.deleted_at", null)
            ->where("t1.id", $cliente_id);
        $limite = $builder->get()->getResultObject();

        if ($limite) {

            $pendiente = $this->getPendienteCobro($cliente_id);
            $credito_disponible = floatval($limite[0]->limite_credito) - $pendiente[0] - $pendiente[1] - floatval($total_pedido);
            if ($credito_disponible < 0)
                return false;
            return true;
        }
        return false;
    }

    public function getClienteDataFacturas($cliente_id)
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "
                t1.nombre AS cliente_nombre, t1.direccion AS cliente_address, t1.cif AS cliente_cif, 
                t2.nombre AS cliente_pais, t1.cp AS cliente_cp, t1.ciudad AS cliente_ciudad, 
                t3.nombre AS cliente_provincia, t1.credito_asegurado AS creditoAsegurado"
            )
            ->where("t1.deleted_at", null)
            ->where("t1.id", $cliente_id);
        $builder->join("lg_paises t2", "t1.pais_id = t2.id", "left");
        $builder->join("lg_provincias t3", "t1.provincia_id = t3.id", "left");

        return $builder->get()->getResultArray();
    }
    public function getClienteDataPresupuestoPedidoFactura(int $cliente_id): array
    {
        $query = $this->db
            ->table($this->table . " t1")
            ->select([
                "t1.id as clienteId",
                "presupuestos.id as presupuestoId",
                "pedidos.id as pedidoId",
                "presupuesto_estados.estado as presupuestoEstado",
                "facturas_pedidos_lineas.factura_id as facturaId",

            ])
            ->join("presupuestos", "t1.id = presupuestos.cliente_id", "left")
            ->join("presupuesto_estados", "presupuestos.estado_id = presupuesto_estados.id", "left")
            ->join("pedidos_linea", "presupuestos.id = pedidos_linea.presupuesto_id", "left")
            ->join("pedidos", "pedidos.id = pedidos_linea.pedido_id", "left")
            ->join("facturas_pedidos_lineas", "facturas_pedidos_lineas.pedido_linea_id = pedidos_linea.id", "left")
            ->where("t1.id", $cliente_id);
        $data = $query->get()->getResultObject();
        $facturas = [];
        $presupuestos = [];
        $pedidos = [];
        $result = [];
        foreach ($data as $row) {
            $facturas[] = $row->facturaId;
            $presupuestos[] = $row->presupuestoId;
            $pedidos[] = $row->pedidoId;
        }
        $result["facturas"] = array_unique(array_filter($facturas));
        $result["presupuestos"] = array_unique(array_filter($presupuestos));
        $result["pedidos"] = array_unique(array_filter($pedidos));

        return $result;
    }


    public function getNombre($id = -1)
    {

        try {

            $builder = $this->db
                ->table($this->table . " t1")
                ->select(
                    "t1.nombre AS nombre"
                )
                ->where("id", $id)
                ->where("t1.deleted_at", null);

            return $builder->get()->getResultObject()[0]->nombre;
        } catch (\Exception $e) {
            return null;
        }
    }

    public function getIdName($search = "")
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.nombre AS name"
            )
            ->where("t1.deleted_at", null);
        return empty($search)
            ? $builder->get()->getResultObject()
            : $builder
            ->groupStart()
            ->like("t1.nombre", $search)
            ->groupEnd()->get()->getResultObject();
    }

    public function getPendienteCobro($cliente_id = -1)
    {

        $pendiente_old = $this->getTotalPendienteOldERP($cliente_id);

        // Subconsulta para verificar la existencia en facturas_lineas
        $subquery_facturas = "(SELECT 1 
            FROM facturas_lineas fl 
            JOIN facturas f2 ON fl.factura_id = f2.id 
            WHERE (fl.pedido_linea_impresion_id = p.id OR fl.pedido_maquetacion_id = p.id) 
            AND f2.estado = 'validada')";

        // Subconsulta para calcular el total de pedidos pendientes
        $subquery_total_pedidos = $this->db->table('pedidos p')
            ->select('SUM(p.total_precio)', false)
            ->join('pedidos_linea pl', 'p.id = pl.pedido_id', 'left')
            ->join('presupuestos pr', 'pl.presupuesto_id = pr.id', 'left')
            ->where('pr.cliente_id', $cliente_id)
            ->whereIn('p.estado', ['produccion', 'finalizado', 'enviado'])
            ->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS
            ->getCompiledSelect();

        // Construcción de la consulta principal
        $builder = $this->db->table('(SELECT DISTINCT cliente_id FROM presupuestos UNION SELECT DISTINCT cliente_id FROM facturas) c');
        $builder->select('c.cliente_id');
        $builder->select('COALESCE(SUM(f.pendiente), 0) AS total_facturas');
        $builder->select("COALESCE(($subquery_total_pedidos), 0) AS total_pedidos", false);
        $builder->select("COALESCE(SUM(f.pendiente), 0) + COALESCE(($subquery_total_pedidos), 0) AS total_pendiente", false);
        $builder->join('facturas f', 'c.cliente_id = f.cliente_id AND f.deleted_at IS NULL AND f.estado = "validada"', 'left');
        $builder->where('c.cliente_id', $cliente_id);
        $builder->groupBy('c.cliente_id');

        $query = $builder->get();
        $valor = $query->getRow();

        if ($valor) {
            return [round(floatval($valor->total_pendiente), 2), round(floatval($pendiente_old), 2)];
        } else {
            return [0, round(floatval($pendiente_old), 2)];
        }
    }

    private function getTotalPendienteOldERP($customer_id = -1)
    {

        $db = \Config\Database::connect('old_erp'); // Conectar a olderp
        $builder = $db->table('safekat.facturas f');
        $builder->select('f.customer_id');
        $builder->select('SUM(f.pendiente) + COALESCE((
            SELECT SUM(pl.total_raw)
            FROM safekat.pedido_libro pl
            WHERE pl.factura_id IS NULL
            AND pl.estado IN ("aceptado", "finalizado", "validado")
            AND pl.total_raw IS NOT NULL
            AND pl.total_raw != 0
            AND pl.inc_rei IS NULL
            AND pl.customer_id = f.customer_id
        ), 0) AS total_pendiente');
        $builder->where('f.deleted_at IS NULL');
        $builder->where('f.estado', 'open');
        $builder->where('f.customer_id', $customer_id);
        $builder->groupBy('f.customer_id');

        $query = $builder->get();
        $valor = $query->getRow();
        /*
        $builder = $db->table('vista_importe_pendiente_cliente t1')
            ->select('*')
            ->where('t1.customer_id', $customer_id);
        $query = $builder->get();
        $valor = $query->getRow();*/
        if ($valor) {
            return $valor->total_pendiente;
        } else {
            return 0;
        }
    }

    public function getResumenPagos($cliente_id = -1)
    {

        $result = [];
        $data = $this->db->table('facturas f')
            ->select('sum(f.pendiente) as total')
            ->where('f.cliente_id', $cliente_id)
            ->where('f.deleted_at IS NULL')
            ->where('f.estado', 'validada')
            ->where('f.estado_pago', 'pendiente')
            ->get()
            ->getResultObject();
        $result['total_facturas_sin_pagar'] =
            round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2);

        $data = $this->db->table('facturas_pagos fp')
            ->select('sum(fp.total) as total')
            ->join('facturas f', 'fp.factura_id = f.id', 'left')
            ->where('f.cliente_id', $cliente_id)
            ->where('f.estado_pago', 'pendiente')
            ->where('fp.fecha_pago_at IS NOT NULL')
            ->where('fp.deleted_at IS NULL')
            ->where('f.deleted_at IS NULL')
            ->get()
            ->getResultObject();
        $result['total_facturas_pagadas'] =
            round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2);

        $data = $this->db->table('facturas f')
            ->select('sum(fp.total) as total')
            ->join('facturas_pagos fp', 'fp.factura_id = f.id', 'left')
            ->where('f.cliente_id', $cliente_id)
            ->where('f.estado_pago', 'pendiente')
            ->where('fp.fecha_vencimiento_at <', date('Y-m-d'))
            ->where('fp.fecha_pago_at IS NULL')
            ->where('f.deleted_at IS NULL')
            ->where('fp.deleted_at IS NULL')
            ->get()
            ->getResultObject();
        $result['total_facturas_vencidas'] =
            round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2);

        // Subconsulta para verificar la existencia en facturas_lineas
        $subquery_facturas = "(SELECT 1 
            FROM facturas_lineas fl 
            JOIN facturas f2 ON fl.factura_id = f2.id 
            WHERE (fl.pedido_linea_impresion_id = p.id OR fl.pedido_maquetacion_id = p.id) 
            AND f2.estado = 'validada')";


        // Subconsulta para calcular el total de pedidos en produccion
        $data = $this->db->table('pedidos p')
            ->select('SUM(p.total_precio) as total', false)
            ->join('pedidos_linea pl', 'p.id = pl.pedido_id', 'left')
            ->join('presupuestos pr', 'pl.presupuesto_id = pr.id', 'left')
            ->where('pr.cliente_id', $cliente_id)
            ->whereIn('p.estado', ['produccion'])
            ->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS
            ->get()
            ->getResultObject();
        $query = $this->db->getLastQuery();
        $result['total_pedidos_produccion'] =
            round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2);

        // Subconsulta para calcular el total de pedidos finalizados
        $data = $this->db->table('pedidos p')
            ->select('SUM(p.total_precio) as total', false)
            ->join('pedidos_linea pl', 'p.id = pl.pedido_id', 'left')
            ->join('presupuestos pr', 'pl.presupuesto_id = pr.id', 'left')
            ->where('pr.cliente_id', $cliente_id)
            ->whereIn('p.estado', ['finalizado', 'enviado'])
            ->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS
            ->get()
            ->getResultObject();
        $result['total_pedidos_finalizados'] =
            round(floatval(($data && $data[0]->total != null) ? $data[0]->total : 0), 2);

        $result['pendiente_old_erp'] = 
            round(floatval($this->getTotalPendienteOldERP($cliente_id)), 2);

        $result['total_pendiente'] =
            $result['total_facturas_sin_pagar']
            + $result['total_pedidos_produccion']
            + $result['total_pedidos_finalizados'] + 
            $result['pendiente_old_erp'];
        $result['total_pendiente'] = round(floatval($result['total_pendiente']), 2);

        $result['limite_credito'] = $this->db->table('clientes')
            ->select('limite_credito')
            ->where('id', $cliente_id)
            ->where("deleted_at", null)
            ->get()
            ->getResultObject()[0]->limite_credito;
        $result['limite_credito'] = round(floatval($result['limite_credito']), 2);

        $result['margen_disponible'] = round(floatval($result['limite_credito'] - $result['total_pendiente']), 2);
        return $result;
    }

    public function querySelectClienteContacto(int $cliente_id, string $q): array
    {
        $query = $this->builder()->select([
            "cliente_contactos.id",
            "CONCAT(COALESCE(cliente_contactos.nombre,''),' ',COALESCE(cliente_contactos.apellidos,'')) as name",
            "COALESCE(cliente_contactos.cargo,'".lang("ClienteContactos.cargo_null")."') as description"
        ])
            ->join('cliente_contactos', 'cliente_contactos.cliente_id = clientes.id', 'left')
            ->where('cliente_contactos.deleted_at',null)
            ->where('cliente_contactos.email is NOT NULL',null,false)
            ->where('cliente_contactos.cliente_id', $cliente_id);
        if ($q) {
            $query->groupStart();
            $query->orLike('name', $q);
            $query->orLike('description', $q);
            $query->groupEnd();
        }
        return $query->get()->getResultArray() ?? [];
    }
}
