<?php

namespace App\Services;

use App\Entities\Configuracion\PapelImpresion;
use App\Models\Configuracion\MaquinasPapelesImpresionModel;
use App\Models\Configuracion\PapelImpresionMargenModel;
use App\Models\Configuracion\PapelImpresionModel;
use App\Models\Configuracion\PapelImpresionTipologiaModel;
use CodeIgniter\Config\BaseService;



class PapelImpresionService extends BaseService
{

    protected PapelImpresionModel $papelImpresionModel;
    protected PapelImpresionTipologiaModel $papelImpresionTipologiaModel;
    protected MaquinasPapelesImpresionModel $maquinaPapelImpresionModel;
    protected PapelImpresionMargenModel $papelImpresionMargenModel;
    protected MaquinasPapelesImpresionModel $papelImpresionMaquinaModel;


    protected PapelImpresion $papelImpresion;


    public function __construct()
    {
        $this->papelImpresionModel = model(PapelImpresionModel::class);
        $this->papelImpresionModel = model(PapelImpresionModel::class);
        $this->papelImpresionMargenModel = model(PapelImpresionMargenModel::class);
        $this->papelImpresionTipologiaModel = model(PapelImpresionTipologiaModel::class);
        $this->papelImpresionMaquinaModel = model(MaquinasPapelesImpresionModel::class);
    }
    public function setPapelImpresionEntity(PapelImpresion $papelImpresionEntity): self
    {
        $this->papelImpresion = $papelImpresionEntity;
        return $this;
    }
    public function duplicate(string $newName): ?PapelImpresion
    {
        $papelImpresionRow = $this->papelImpresion?->toArray();
        $papelImpresionMargenes = $this->papelImpresion->margen();
        $papelImpresionTipologias = $this->papelImpresion->tipologia();
        $papelImpresionMaquinas = $this->papelImpresion->maquinas_impresion();

        $papelImpresionRow['nombre'] = $newName;
        if ($this->papelImpresion) {
            unset($papelImpresionRow["id"]);
            $papelImpresionDuplicatedId = $this->papelImpresionModel->insert($papelImpresionRow);
        }
        if ($papelImpresionMargenes) {
            foreach ($papelImpresionMargenes as $papelImpresionMargen) {
                unset($papelImpresionMargen["id"]);
                $papelImpresionMargen['papel_impresion_id'] = $papelImpresionDuplicatedId;
                $this->papelImpresionMargenModel->insert($papelImpresionMargen);
            }
        }
        if ($papelImpresionTipologias) {
            foreach ($papelImpresionTipologias as $papelImpresionTipologia) {
                unset($papelImpresionTipologia["id"]);
                $papelImpresionTipologia['papel_impresion_id'] = $papelImpresionDuplicatedId;
                $this->papelImpresionTipologiaModel->insert($papelImpresionTipologia);
            }
        }
        if ($papelImpresionMaquinas) {
            foreach ($papelImpresionMaquinas as $papelImpresionMaquina) {
                unset($papelImpresionMaquina["id"]);
                $papelImpresionMaquina['papel_impresion_id'] = $papelImpresionDuplicatedId;
                $this->papelImpresionMaquinaModel->insert($papelImpresionMaquina);
            }
        }
        $papelImpresionDuplicated = $this->papelImpresionModel->find($papelImpresionDuplicatedId);
        return $papelImpresionDuplicated;
    }
}
