<?php

namespace App\Entities\Configuracion;

use CodeIgniter\Entity\Entity;
use App\Entities\Tarifas\Acabados\TarifaAcabadoEntity;
use App\Entities\Tarifas\TarifaManipuladoEntity;
use App\Models\Configuracion\ServicioClienteModel;
use CodeIgniter\Database\MySQLi\Result;

class ServicioClienteEntity extends Entity
{
    protected $attributes = [
        "nombre" => null,
        "code" => null,
    ];
    protected $casts = [
        "nombre" => "string",
        "code" => "string",
    ];

    public function withManipuladoTarifas()
    {
        $this->attributes["tarifas_manipulado"] = $this->getManipuladoTarifas();
        return $this;
    }
    public function withAcabadoTarifas()
    {
        $this->attributes["tarifas_acabado"] = $this->getAcabadoTarifas();
        return $this;
    }
    public function withAllTarifas(){
        $this->withManipuladoTarifas();
        $this->withAcabadoTarifas();
        return $this;
    }
    public function getManipuladoTarifas(): ?object
    {
        $model = model(ServicioClienteModel::class);
        return $model->builder()->select("lg_tarifa_manipulado.*")
        ->join("servicio_cliente_tareas","servicio_cliente_tareas.servicio_cliente_id = servicios_cliente.id",'left')
        ->join("lg_tarifa_manipulado","lg_tarifa_manipulado.id = servicio_cliente_tareas.tarifa_manipulado_id",'left')
        ->where("servicio_cliente_tareas.servicio_cliente_id",$this->attributes["id"])
        ->where("lg_tarifa_manipulado.id IS NOT NULL",NULL,FALSE)
        ->where("servicio_cliente_tareas.deleted_at", NULL) // Exclude soft-deleted rows
        ->get()->getFirstRow();
    }
    public function getAcabadoTarifas(): ?object
    {
        $model = model(ServicioClienteModel::class);
        return $model->builder()->select("lg_tarifa_acabado.*")
        ->join("servicio_cliente_tareas","servicio_cliente_tareas.servicio_cliente_id = servicios_cliente.id",'left')
        ->join("lg_tarifa_acabado","lg_tarifa_acabado.id = servicio_cliente_tareas.tarifa_acabado_id",'left')
        ->where("servicio_cliente_tareas.servicio_cliente_id",$this->attributes["id"])
        ->where("servicio_cliente_tareas.deleted_at", NULL) // Exclude soft-deleted rows
        ->where("lg_tarifa_acabado.id IS NOT NULL",NULL,FALSE)
        ->get()->getFirstRow();
    }
}
