<?php
declare(strict_types = 1)
	;

namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\DashboardDAO;
use App\DAO\PacienteDAO;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\GeneralCollection;
use App\Utils\Collections\QueryFilterCollection;

class DashboardService extends Service {

	public function getDashboardMedico(int $id_medico, QueryFilterCollection $query_filter_collection, $query_order_collection): ServiceResponse {

		$dashboard_dao = new DashboardDAO();
		if ($query_filter_collection->length > 0) {
			$dao_response = $dashboard_dao->getAgendasSearch( $id_medico, $query_filter_collection, $query_order_collection );
		} else {
			$dao_response = $dashboard_dao->getAgendasDoDia( $id_medico, $query_filter_collection );
		}

		$service_response = new ServiceResponse();
		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$dashboard_medico_vm_collection = (function (GeneralCollection $general_collection): GeneralCollection {
					return $general_collection;
				})( $dao_response->getData() );
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $dashboard_medico_vm_collection->toArray() );
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response->setStatus( ServiceResponseInterface::RECORD_NOT_FOUND );
				break;
			default :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				break;
		}
		return $service_response;

	}

    public function getDashboardMedicoTelevet(int $id_medico, QueryFilterCollection $query_filter_collection, $query_order_collection): ServiceResponse {

        $dashboard_dao = new DashboardDAO();
        if ($query_filter_collection->length > 0) {
            $dao_response = $dashboard_dao->getAgendasSearchTelevet( $id_medico, $query_filter_collection, $query_order_collection );
        } else {
            $dao_response = $dashboard_dao->getAgendasDoDiaTelevet( $id_medico, $query_filter_collection );
        }

        $service_response = new ServiceResponse();
        switch ($dao_response->getStatus()) {
            case DAOResponseInterface::SUCESS :
                $dashboard_medico_vm_collection = (function (GeneralCollection $general_collection): GeneralCollection {
                    return $general_collection;
                })( $dao_response->getData() );
                $service_response->setStatus( ServiceResponseInterface::SUCESS );
                $service_response->setData( $dashboard_medico_vm_collection->toArray() );
                break;
            case DAOResponseInterface::RECORD_NOT_FOUND :
                $service_response->setStatus( ServiceResponseInterface::RECORD_NOT_FOUND );
                break;
            default :
                $service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
                break;
        }
        return $service_response;

    }

	public function getDashboardPaciente(int $id_paciente): ServiceResponse {

		$service_response = new ServiceResponse();
		$jwtPayload = JWTService::getTokenJWTPayload();
		$user_id_paciente = (new PacienteDAO())->getPacienteIDFromUsuarioID( $jwtPayload->getUid() );
		if ($id_paciente !== $user_id_paciente) {
			$service_response->setStatus( ServiceResponseInterface::FORBIDDEN );
			return $service_response;
		}
		$dashboard_dao = new DashboardDAO();
		$dao_response = $dashboard_dao->getHorariosAgendados( $id_paciente );

		switch ($dao_response->getStatus()) {
			case DAOResponseInterface::SUCESS :
				$dashboard_paciente_vm_collection = $dao_response->getData();
				$service_response->setStatus( ServiceResponseInterface::SUCESS );
				$service_response->setData( $dashboard_paciente_vm_collection->toArray() );
				break;
			case DAOResponseInterface::RECORD_NOT_FOUND :
				$service_response->setStatus( ServiceResponseInterface::RECORD_NOT_FOUND );
				break;
			default :
				$service_response->setStatus( ServiceResponseInterface::DATABASE_CONNECTION_ERROR );
				break;
		}
		return $service_response;

	}

}
