<?php

namespace App\Controllers\Tarifas\Maquinas;

use App\Controllers\BaseController;
use App\Models\Configuracion\MaquinaModel;
use App\Models\Configuracion\MaquinaTareaModel;
use App\Models\Tarifas\Maquinas\TarifaAcabadoMaquinaModel;
use App\Models\Tarifas\Maquinas\TarifaManipuladoMaquinaModel;
use App\Models\Tarifas\Maquinas\TarifaEncuadernacionMaquinaModel;
use App\Models\Tarifas\Maquinas\TarifaPreimpresionMaquinaModel;
use App\Models\Tarifas\Maquinas\TarifaExtraMaquinaModel;

use App\Services\TarifaMaquinaService;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation;
use Hermawan\DataTables\DataTable;
use Psr\Log\LoggerInterface;


class TarifaMaquinas extends BaseController
{
    protected $format = 'json';
    protected array $viewData = [];
    protected MaquinaModel $maquinaModel;
    protected MaquinaTareaModel $maquinaTareaModel;
    protected TarifaMaquinaService $tarifaMaquinaService;
    protected  Validation $validation;
    protected static $controllerSlug = "tarifa-maquinas";
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        $this->maquinaModel = model(MaquinaModel::class);
        $this->maquinaTareaModel = model(MaquinaTareaModel::class);
        $this->tarifaMaquinaService = service("tarifa_maquina");
        $this->validation = service("validation");
        parent::initController($request, $response, $logger);
    }
    /**========================================================================
     *                           CREATE
     *========================================================================**/

    public function add_tarifa_acabado_maquina_with_tarea()
    {
        $bodyData = $this->request->getPost();
        $validated =  $this->validation->run($bodyData, "tarifa_maquina_acabado");
        if ($validated) {
            $r = $this->tarifaMaquinaService->attachAcabadoToMaquina(...$bodyData);
            return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => true, "data" => $r]);
        } else {
            return $this->response->setJSON($this->validation->getErrors())->setStatusCode(400);
        }
    }
    public function add_tarifa_manipulado_maquina_with_tarea()
    {
        $bodyData = $this->request->getPost();
        $validated =  $this->validation->run($bodyData, "tarifa_maquina_manipulado");
        if ($validated) {
            $r = $this->tarifaMaquinaService->attachManipuladoToMaquina(...$bodyData);
            return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => true, "data" => $r]);
        } else {
            return $this->response->setJSON(["errors" => $this->validation->getErrors()])->setStatusCode(400);
        }
    }
    public function add_tarifa_preimpresion_maquina_with_tarea()
    {
        $bodyData = $this->request->getPost();
        $validated =  $this->validation->run($bodyData, "tarifa_maquina_preimpresion");
        if ($validated) {

            $r = $this->tarifaMaquinaService->attachPreimpresionToMaquina(...$bodyData);
            return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => true, "data" => $r]);
        } else {
            return $this->response->setJSON(["errors" => $this->validation->getErrors()])->setStatusCode(400);
        }
    }
    public function add_tarifa_encuadernacion_maquina_with_tarea()
    {
        $bodyData = $this->request->getPost();
        $validated =  $this->validation->run($bodyData, "tarifa_maquina_encuadernacion");
        if ($validated) {

            $r = $this->tarifaMaquinaService->attachEncuadernacionToMaquina(...$bodyData);
            return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => true, "data" => $r]);
        } else {
            return $this->response->setJSON(["errors" => $this->validation->getErrors()])->setStatusCode(400);
        }
    }
    public function add_tarifa_extra_maquina_with_tarea()
    {
        $bodyData = $this->request->getPost();
        $validated =  $this->validation->run($bodyData, "tarifa_maquina_encuadernacion");
        if ($validated) {
            $r = $this->tarifaMaquinaService->attachExtraToMaquina(...$bodyData);
            return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => true, "data" => $r]);
        } else {
            return $this->response->setJSON(["errors" => $this->validation->getErrors()])->setStatusCode(400);
        }
    }
    /**========================================================================
     *                           DELETES
     *========================================================================**/
    public function delete_tarifa_acabado_maquina_with_tarea($tarifa_acabado_maquina_id)
    {
        $m = model(TarifaAcabadoMaquinaModel::class);
        $r = $m->delete($tarifa_acabado_maquina_id);
        return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => true, "data" => $r]);

    }
    public function delete_tarifa_manipulado_maquina_with_tarea($tarifa_manipulado_maquina_id)
    {
        $m = model(TarifaManipuladoMaquinaModel::class);
        $r = $m->delete($tarifa_manipulado_maquina_id);
        return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => true, "data" => $r]);

    }
    public function delete_tarifa_encuadernacion_maquina_with_tarea($tarifa_encuadernacion_maquina_id)
    {
        $m = model(TarifaEncuadernacionMaquinaModel::class);
        $r = $m->delete($tarifa_encuadernacion_maquina_id);
        return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => true, "data" => $r]);

    }
    public function delete_tarifa_preimpresion_maquina_with_tarea($tarifa_preimpresion_maquina_id)
    {
        $m = model(TarifaPreimpresionMaquinaModel::class);
        $r = $m->delete($tarifa_preimpresion_maquina_id);
        return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => true, "data" => $r]);

    }
    public function delete_tarifa_extra_maquina_with_tarea($tarifa_extra_maquina_id)
    {
        $m = model(TarifaExtraMaquinaModel::class);
        $r = $m->delete($tarifa_extra_maquina_id);
        return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => true, "data" => $r]);

    }

    /**========================================================================
     *                           SELECTS
     *========================================================================**/
    public function get_select_maquina_acabado()
    {
        $r = $this->maquinaModel->getSelectQuery(q: $this->request->getGet("q"), type: 'acabado');
        return $this->response->setJSON($r);
    }
    public function get_select_maquina_impresion()
    {
        $r = $this->maquinaModel->getSelectQuery(q: $this->request->getGet("q"), type: 'impresion');
        return $this->response->setJSON($r);
    }
    public function get_select_maquina_manipulado()
    {
        $r = $this->maquinaModel->getSelectQuery(q: $this->request->getGet("q"), type: 'manipulado');
        return $this->response->setJSON($r);
    }
    public function get_select_maquina_tareas()
    {
        $r = $this->maquinaTareaModel->getSelectQuery(q: $this->request->getGet("q"));
        return $this->response->setJSON($r);
    }
    /**========================================================================
     *                           DATATABLES
     *========================================================================**/
    public function get_datatable_tarifa_acabado_maquina(int $tarifa_acabado_id)
    {
        $m = model(TarifaAcabadoMaquinaModel::class);
        $q = $m->queryDatatable($tarifa_acabado_id);
        return DataTable::of($q)
            ->add("action", fn($q) => $q->id)
            ->toJson(true);
    }
    public function get_datatable_tarifa_manipulado_maquina(int $tarifa_manipulado_id)
    {
        $m = model(TarifaManipuladoMaquinaModel::class);
        $q = $m->queryDatatable($tarifa_manipulado_id);
        return DataTable::of($q)
            ->add("action", fn($q) => $q->id)
            ->toJson(true);
    }
    public function get_datatable_tarifa_preimpresion_maquina(int $tarifa_preimpresion_id)
    {
        $m = model(TarifaPreimpresionMaquinaModel::class);
        $q = $m->queryDatatable($tarifa_preimpresion_id);
        return DataTable::of($q)
            ->add("action", fn($q) => $q->id)
            ->toJson(true);
    }
    public function get_datatable_tarifa_encuadernacion_maquina(int $tarifa_encuadernacion_id)
    {
        $m = model(TarifaEncuadernacionMaquinaModel::class);
        $q = $m->queryDatatable($tarifa_encuadernacion_id);
        return DataTable::of($q)
            ->add("action", fn($q) => $q->id)
            ->toJson(true);
    }
    public function get_datatable_tarifa_extra_maquina(int $tarifa_extra_id)
    {
        $m = model(TarifaExtraMaquinaModel::class);
        $q = $m->queryDatatable($tarifa_extra_id);
        return DataTable::of($q)
            ->add("action", fn($q) => $q->id)
            ->toJson(true);
    }
}
