<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use app\Validations\VoucherValidation;
use App\Services\VoucherService;
use App\Services\ServiceResponse\ServiceResponseInterface;

class VoucherController extends BaseController {

	public function registerVoucher(): Response {

		$dados = $this->getDataPost();

		$validar = array ();
		$validar = VoucherValidation::register( $dados );
		if (count( $validar ) > 0) {
			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'erros' => $validar
			] );
		}

		$service = new VoucherService();

		$response_service = $service->registerVoucher( $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' => 'Loja cadastrada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::HAVE_CONFLICTS => [ 
				'code' => ResponseInterface::HTTP_CONFLICT,
				'message' => 'Loja já exite para a unidade.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function changeVoucherStatus(): Response {

		$dados = $this->getDataPost();

		$validar = array ();
		$validar = VoucherValidation::changestatus( $dados );
		if (count( $validar ) > 0) {
			return $this->response->setStatusCode( 400 )
				->setJSON( [ 
				'message' => 'Informe os dados corretamente.',
				'erros' => $validar
			] );
		}

		$service = new VoucherService();

		$response_service = $service->changeVoucherStatus( $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' => 'Alteração de status 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 conflitantes.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Não Foram encontrados registros.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getVoucherById(int $id_voucher): Response {

		$service = new VoucherService();

		$response_service = $service->getVoucherById( $id_voucher );

		$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' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Nenhum registro encntrado.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getVoucherListByPetId(int $id_pet): Response {

		$service = new VoucherService();

		$response_service = $service->getVoucherListByPetId( $id_pet );

		$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' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Nenhum registro encntrado.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getVoucherListByUnidadeId(int $id_unidade): Response {

		$service = new VoucherService();

		$response_service = $service->getVoucherListByUnidadeId( $id_unidade );

		$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' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Nenhum registro encntrado.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

	public function getVoucherBySerial($serial): Response {

		$service = new VoucherService();

		$response_service = $service->getVoucherBySerial( $serial );

		$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' => 'Pesquisa realizada com sucesso.'
			],
			ServiceResponseInterface::INSUFFICIENT_DATA => [ 
				'code' => ResponseInterface::HTTP_BAD_REQUEST,
				'message' => 'Informe os dados corretamente.'
			],
			ServiceResponseInterface::RECORD_NOT_FOUND => [ 
				'code' => ResponseInterface::HTTP_NO_CONTENT,
				'message' => 'Nenhum registro encntrado.'
			]
		];
		return $this->getResponse( $response_service, $response_messages );

	}

}

