<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use Exception;
use App\Services\PagSeguroService;
use App\Services\ServiceResponse\ServiceResponseInterface;

class PagSeguroController extends BaseController {

	public function registerNotification(): Response {

		$dados = $this->getDataPost();
		$json = json_encode($dados);
		$text = getenv('pgs.token').'-'.$json;
		$signature = hash('sha256', $text);
		$headers = $this->request->getHeaders();
		$auth_token='';
		
		if(isset($headers['X-Authenticity-Token'])){
			$auth_token = $headers['X-Authenticity-Token']->getValue();
		}
		if(isset($headers['x-authenticity-token'])){
			$auth_token = $headers['x-authenticity-token']->getValue();
		}
		
		if($signature != $auth_token) {
			return $this->response->setStatusCode( 400 )
			->setJSON( [
					'message' => 'Informe os dados corretamente.',
					'erros' => 'Assinatura inválida'
			] );
		}
		$service = new PagSeguroService();
		$response_service = $service->registerNotification( $dados );

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Notificação porcessada com sucesso.'
			],
			ServiceResponseInterface::VALIDATION_PARAMS_ERROR => [ 
				'code' => ResponseInterface::HTTP_FAILED_DEPENDENCY,
				'message' => 'Compra não aprovada pela operadora.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::HAVE_CONFLICTS => [ 
				'code' => ResponseInterface::HTTP_CONFLICT,
				'message' => 'Dados informado em conflito.'
			],
			ServiceResponseInterface::UNKNOWN_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro Interno.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getKey(): Response {

		$service = new PagSeguroService();

		$response_service = $service->getKey();

		$response_messages = [ 
			ServiceResponseInterface::DATABASE_CONNECTION_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro de conexão com o banco de dados.'
			],
			ServiceResponseInterface::SUCESS => [ 
				'code' => ResponseInterface::HTTP_OK,
				'message' => 'Cosulta realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::HAVE_CONFLICTS => [ 
				'code' => ResponseInterface::HTTP_CONFLICT,
				'message' => 'Dados informado em conflito.'
			],
			ServiceResponseInterface::UNKNOWN_ERROR => [ 
				'code' => ResponseInterface::HTTP_INTERNAL_SERVER_ERROR,
				'message' => 'Erro Interno.'
			]
		];

		return $this->getResponse( $response_service, $response_messages );

	}

}

