<?php

namespace App\Controllers\Configuracion;

use App\Controllers\BaseController;
use App\Controllers\BaseResourceController;
use App\Entities\Configuracion\FestivoEntity;
use App\Models\Collection;

use App\Entities\Configuracion\Imposicion;
use App\Models\Configuracion\FestivoModel;
use App\Models\Configuracion\ImposicionEsquemaModel;
use App\Models\Configuracion\ImposicionModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation;
use Hermawan\DataTables\DataTable;
use Psr\Log\LoggerInterface;

class FestivoController extends BaseController
{

    protected $modelName = FestivoModel::class;
    protected FestivoModel $model;
    protected static $controllerSlug = 'festivos';
    protected $format = 'json';

    protected string $viewPath = 'themes/vuexy/form/configuracion/festivos/';

    protected $indexRoute = 'festivoList';
    protected array $viewData = [];
    protected Validation $validation;

    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        $this->viewData['pageTitle'] = lang('Festivos.moduleTitle');
        $this->viewData['usingSweetAlert'] = true;
        $this->model = model($this->modelName);
        $this->validation = service("validation");
        parent::initController($request, $response, $logger);
    }

    public function index()
    {
        return view($this->viewPath . $this->indexRoute);
    }

    public function store_festivo_date()
    {
        $bodyData = $this->request->getPost();
        $date = $bodyData['date'];
        $count = $this->model->where('date',$date)->countAllResults();
        if ($count) {
            $status = $this->model->where('date', $date)->delete(purge: true);
            return $this->response->setJSON([
                "message" => lang("App.user_alert_delete"),
                "status" => $status,
            ]);
        } else {

            $status = $this->model->insert($bodyData);
            if ($status) {
                $festivoEntity = $this->model->find($status);
                return $this->response->setJSON([
                    "message" => lang("App.global_alert_save_success"),
                    "status" => $status,
                    "data" => $festivoEntity
                ]);
            } else {
                return $this->response->setJSON([
                    "message" => lang("App.global_alert_save_error"),
                    "errors" => $this->model->errors(),
                    "status" => true
                ])->setStatusCode(400);
            }
        }
    }
    public function delete_festivo_date($id)
    {
        $status = $this->model->delete($id, true);
        if ($status) {
            return $this->response->setJSON([
                "message" => lang("App.user_alert_delete"),
                "status" => $status,
            ]);
        } else {
            return $this->response->setJSON([
                "message" => lang("App.global_alert_save_error"),
                "errors" => $this->model->errors(),
                "status" => true
            ])->setStatusCode(400);
        }
    }
    public function find_all()
    {
        $festivos = $this->model->findAll();
        return $this->response->setJSON([
            "message" => lang("App.global_alert_fetch_success"),
            "status" => true,
            "data" => $festivos
        ]);
    }
}
