<?php

defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/RestController.php';

use chriskacerguis\RestServer\RestController;

require APPPATH . 'libraries/Format.php';

class Roles extends RestController {

    public function __construct() {
        parent::__construct();
        $this->load->model('roles_model');
    }

    public function get_all_roles_get() {
        $roles = $this->roles_model->get_all_roles();
        $this->load->model(array('usuarios_model', 'modulos_acciones_roles_model'));
        foreach ($roles as &$rol) {
            $rol['usuarios'] = $this->usuarios_model->get_all_usuarios_by_rol_id($rol['id']);
            $rol['acciones_permitidas'] = $this->modulos_acciones_roles_model->get_all_modulos_acciones_activas_by_rol_id($rol['id']);
            unset($rol);
        }
        $data["roles"] = $roles;
        $this->response($data, RestController::HTTP_OK);
    }

    public function get_all_roles_activos_get() {
        $roles = $this->roles_model->get_all_roles_activos();
        $this->response(["roles" => $roles], RestController::HTTP_OK);
    }

    public function get_rol_by_id_get() {
        $this->_params_validation_id();
        $rol = $this->roles_model->get_rol_by_id($this->get('id'));
        if ($rol !== NULL) {
            $this->response(["rol" => $rol], RestController::HTTP_OK);
        } else {
            $this->response(['message' => "Error al obtener el rol, contacte a soporte técnico."], RestController::HTTP_NOT_FOUND);
        }
    }

    public function create_post() {
        $this->_form_validation_create();
        if ($this->roles_model->insert($this->_fill_data_insert())) {
            $this->response(["message" => "Rol guardado."], RestController::HTTP_OK);
        } else {
            $this->response(['message' => 'Error al guardar el registro, contacte a soporte técnico.'], RestController::HTTP_INTERNAL_ERROR);
        }
    }

    public function update_post() {
        $this->_params_validation_id();
        $this->_form_validation_update();
        if ($this->roles_model->update($this->get('id'), $this->_fill_data_update())) {
            $this->response(["message" => "Rol guardado."], RestController::HTTP_OK);
        } else {
            $this->response(['message' => 'Error al guardar el registro, contacte a soporte técnico.'], RestController::HTTP_INTERNAL_ERROR);
        }
    }

    private function _fill_data_insert() {
        $data = array();
        $data['rol_nombre'] = $this->post('rol_nombre');
        $data['status'] = 1;
        $data['created_at'] = date("Y-m-d H:i:s");
        return $data;
    }

    private function _fill_data_update() {
        $data = array();
        $data['rol_nombre'] = $this->post('rol_nombre');
        $data['updated_at'] = date("Y-m-d H:i:s");
        return $data;
    }

    public function is_unique_rol_create() {
        $rol_nombre = $this->post('rol_nombre');
        $rol = $this->roles_model->check_rol_is_unique_in_sistema_create($rol_nombre);
        if ($rol !== NULL) {
            $status = ($rol['status'] === "1") ? "<strong>ACTIVO</strong>" : "<strong>INACTIVO</strong>";
            $this->form_validation->set_message('is_unique_rol_create', 'Ya existe un rol ' . $status . ' con ese nombre');
            return FALSE;
        }
        return TRUE;
    }

    public function is_unique_rol_update() {
        $rol_nombre = $this->post('rol_nombre');
        $rol = $this->roles_model->check_rol_is_unique_in_sistema_update($rol_nombre, $this->get('id'));
        if ($rol !== NULL) {
            $status = ($rol['status'] === "1") ? "<strong>ACTIVO</strong>" : "<strong>INACTIVO</strong>";
            $this->form_validation->set_message('is_unique_rol_update', 'Ya existe un rol ' . $status . ' con ese nombre');
            return FALSE;
        }
        return TRUE;
    }

    private function _params_validation_id() {
        $this->load->library('params_validation');
        $this->params_validation->set_rules('id', 'ID', 'trim|required|is_natural_no_zero');
        if (!$this->params_validation->run()) {
            $this->response(array('message' => params_validation_errors()), RestController::HTTP_BAD_REQUEST);
        }
    }

    private function _form_validation_create() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('rol_nombre', 'rol_nombre', 'trim|required|max_length[255]|callback_is_unique_rol_create');
        if (!$this->form_validation->run()) {
            $this->response(array('message' => validation_errors()), RestController::HTTP_BAD_REQUEST);
        }
    }

    private function _form_validation_update() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('rol_nombre', 'rol_nombre', 'trim|required|max_length[250]|callback_is_unique_rol_update');
        if (!$this->form_validation->run()) {
            $this->response(array('message' => validation_errors()), RestController::HTTP_BAD_REQUEST);
        }
    }
}
