<?php


namespace App\DAO;



use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\ViewModels\Logsys\LogsysRegistrationViewModel\LogsysRegistrationViewModel;


class LogsysDAO extends DAO
{

//     public function getAdendoListByConsultaId(int $consulta_id) : DAOResponse
//     {
//         $dao_response = new DAOResponse();

//         $sql = "SELECT * FROM adendo WHERE consulta_id=:consulta_id:";
//         $binds = ["consulta_id" => $consulta_id];

//         $adendoQuery = $this->db->query($sql, $binds);

//         $error = $this->db->error();
//         if ($error && $error['code']) {
//             $status_persistence = $error['code'];
//             $dao_response_errors = [];
//             $dao_response->setErrors($dao_response_errors);
//             log_message('error', json_encode($error));
//         } else {
//             $status_persistence = true
//                 ? DAOResponseInterface::SUCESS
//                 : DAOResponseInterface::DATABASE_CONNECTION_ERROR;
//         }

//         $this->db->close();
//         $dao_response->setStatus($status_persistence);
//         if ($status_persistence === DAOResponseInterface::SUCESS) {
//             $dao_response->setData([
//                 "adendos" => $adendoQuery->getResultArray()
//             ]);
//         }
//         return $dao_response;
//     }

	public function registerLog (LogsysRegistrationViewModel $obj) : DAOResponse {
    	
        $dao_response = new DAOResponse();
        $sql ='';
        $binds= [];
        
        if($obj->getId_usuario() == 0) {
        	$sql = <<<SQL
			insert into logsys
			(data,time,http_verb,controller,endpoint,observacao) values
			(curdate(),curtime(),:verbhttp:,:controller:,:endpoint:,:observacao:)
		SQL;
        	$binds = [        			
        			"verbhttp" => $obj->getHttp_verb(),
        			"controller" => $obj->getController(),
        			"endpoint" => $obj->getEndpoint(),
        			"observacao" => $obj->getObservacao()
        	];
        	
        } else {
        	$sql = <<<SQL
			insert into logsys
			(data,time,id_usuario,http_verb,controller,endpoint,observacao) values
			(curdate(),curtime(),:usuarioid:,:verbhttp:,:controller:,:endpoint:,:observacao:)
		SQL;
        	$binds = [
        			"usuarioid" => $obj->getId_usuario(),
        			"verbhttp" => $obj->getHttp_verb(),
        			"controller" => $obj->getController(),
        			"endpoint" => $obj->getEndpoint(),
        			"observacao" => $obj->getObservacao()
        	];
        	
        }

        $logQuery = $this->db->query($sql, $binds);

        $error = $this->db->error();
        if ($error && $error['code']) {
            $status_persistence = $error['code'];
            $dao_response_errors = [];
            $dao_response->setErrors($dao_response_errors);
            log_message('error', json_encode($error));
        } else {
            $status_persistence = true
                ? DAOResponseInterface::SUCESS
                : DAOResponseInterface::DATABASE_CONNECTION_ERROR;
        }
        $this->db->close();
        $dao_response->setStatus($status_persistence);
        if ($status_persistence === DAOResponseInterface::SUCESS) {
        	$dao_response->setData($logQuery);
        }
        return $dao_response;
    }

}
