<?php

namespace App\Services;

use App\Services\Service;
use App\Services\ServiceResponse\ServiceResponse;
use App\DAO\LojaDAO;
use App\Mappers\LojaMapper;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\DAO\LojaCompraDAO;

class LojaService extends Service {

	public function registerLoja(array $dados): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$lojaRegistration_vm = LojaMapper::getLojaRegistrationViewModelFromArray( $dados );

		$lojaDao = new LojaDAO();

		$response_exist = $lojaDao->isExitsLojaToUnidadeId( $lojaRegistration_vm );

		if ($response_exist->getStatus() == DAOResponseInterface::SUCESS) {
			$serviceReponse->setStatus( ServiceResponseInterface::HAVE_CONFLICTS );
			$serviceReponse->setData( [ ] );
			return $serviceReponse;
		}

		if ($response_exist->getStatus() == DAOResponseInterface::DATABASE_CONNECTION_ERROR) {
			$serviceReponse->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
			$serviceReponse->setErrors( $response_exist->getErrors() );
			$serviceReponse->setData( [ ] );
			return $serviceReponse;
		}

		if ($response_exist->getStatus() == DAOResponseInterface::RECORD_NOT_FOUND) {
			$daoResponse = $lojaDao->registerLoja( $lojaRegistration_vm );
			$serviceReponse = $this->setServiceResponse( $daoResponse );
		}

		return $serviceReponse;

	}

	public function changeLojaStatus(array $dados): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$lojaChangeStatus_vm = LojaMapper::getLojaChangeStatusViewModelFromArray( $dados );

		$lojaDao = new LojaDAO();
		$daoResponse = $lojaDao->changeStatusLoja( $lojaChangeStatus_vm );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getLojas(): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$lojaDao = new LojaDAO();
		$daoResponse = $lojaDao->getLojas();
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}

	public function getComprasByUsuarioId(int $idusuario, int $idstatus): ServiceResponse {

		$serviceReponse = new ServiceResponse();

		$lojaCompraDao = new LojaCompraDAO();
		$daoResponse = $lojaCompraDao->getComprasByUsuarioId( $idusuario, $idstatus );
		$serviceReponse = $this->setServiceResponse( $daoResponse );

		return $serviceReponse;

	}
	
	public function getComprasByLojaId(int $id_loja, int $id_status_compra): ServiceResponse {
		
		$serviceReponse = new ServiceResponse();
		
		$lojaCompraService = new LojaCompraService();
		$response = $lojaCompraService->getComprasByLojaId( $id_loja, $id_status_compra );
		$serviceReponse = $response;
		
		return $serviceReponse;
		
	}
	
	public function getIdsCompras(array $compras): array {
					
		$lojaCompraService = new LojaCompraService();
		$response = $lojaCompraService->getIdsCompras( $compras );
		$serviceReponse = $response;
		
		return $response;
		
		
	}
	
	public function getPetsByComprasId(array $compras_id): ServiceResponse {
		
		$serviceReponse = new ServiceResponse();
		
		$lojaCompraService = new LojaCompraService();
		$serviceReponse  =$lojaCompraService->getPetsByComprasId( $compras_id );
		
		
		return $serviceReponse;
		
	}

}

