<?php namespace App\Services;

use App\DAO\DAOResponse\DAOResponseInterface;
use App\DAO\Filter\QueryFilter;
use App\DAO\Pagination\Pagination;
use App\DAO\FilaEspecialidadeDAO;
use App\Mappers\FilaEspecialidadeMapper;
use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use App\Utils\Collections\QueryFilterCollection;
use App\Utils\Collections\QueryOrderCollection;

class FilaEspecialidadeService extends Service
{
    public function getFilaEspecialidadeList (?Pagination $pagination, ?QueryFilterCollection $query_filter_collection, ?QueryOrderCollection $query_order_collection) : ServiceResponse{
        $fila_dao = new FilaEspecialidadeDAO();
        $dao_response = $fila_dao->getFilaEspecialidadeList($pagination, $query_filter_collection, $query_order_collection);
        switch ($dao_response->getStatus()) {
            case DAOResponseInterface::SUCESS:
                $service_response_status = ServiceResponseInterface::SUCESS;
                break;
            case DAOResponseInterface::RECORD_NOT_FOUND:
                $service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
                break;
            case DAOResponseInterface::DATABASE_CONNECTION_ERROR:
                $service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
                break;
        }
        $service_response = ServiceResponse::builder()
            ->setStatus($service_response_status)
            ->build();
        if ($service_response_status === DAOResponseInterface::SUCESS) {
            $dao_response_data = $dao_response->getData();
            $fila_details_array = FilaEspecialidadeMapper::getFilaEspecialidadesDetailsArrayFromFilaEspecialidadeDetailsVMCollection($dao_response_data);
            $pagination = $dao_response->getPagination();
            $data = [
                'filas' => $fila_details_array,
                'pagination' => $pagination ? $pagination->toResponseArray() : null
            ];
            $service_response->setData($data);
        }
        return $service_response;
    }

    public function getFilaEspecialidade (QueryFilter $query_filter) : ServiceResponse {
        $fila_dao = new FilaEspecialidadeDAO();
        $query_filter_collection = new QueryFilterCollection();
        $query_filter_collection->push($query_filter);
        $dao_response = $fila_dao->getFilaEspecialidade($query_filter_collection);
        $service_response_status = [
            DAOResponseInterface::SUCESS => ServiceResponseInterface::SUCESS,
            DAOResponseInterface::RECORD_NOT_FOUND => ServiceResponseInterface::RECORD_NOT_FOUND,
            DAOResponseInterface::DATABASE_CONNECTION_ERROR => ServiceResponseInterface::DATABASE_CONNECTION_ERROR,
        ][$dao_response->getStatus()];
        $service_response = ServiceResponse::builder()
            ->setStatus($service_response_status ?? ServiceResponseInterface::UNKNOWN_ERROR)
            ->build();
        if ($service_response_status === ServiceResponseInterface::SUCESS) {
            $service_response->setData($dao_response->getData()->toArray());
        }
        return $service_response;
    }



    public function getFilaClienteList (?Pagination $pagination, ?QueryFilterCollection $query_filter_collection, ?QueryOrderCollection $query_order_collection) : ServiceResponse{
        $fila_dao = new FilaEspecialidadeDAO();
        $dao_response = $fila_dao->getFilaClienteList($pagination, $query_filter_collection, $query_order_collection);
        switch ($dao_response->getStatus()) {
            case DAOResponseInterface::SUCESS:
                $service_response_status = ServiceResponseInterface::SUCESS;
                break;
            case DAOResponseInterface::RECORD_NOT_FOUND:
                $service_response_status = ServiceResponseInterface::RECORD_NOT_FOUND;
                break;
            case DAOResponseInterface::DATABASE_CONNECTION_ERROR:
                $service_response_status = ServiceResponseInterface::DATABASE_CONNECTION_ERROR;
                break;
        }
        $service_response = ServiceResponse::builder()
            ->setStatus($service_response_status)
            ->build();
        if ($service_response_status === DAOResponseInterface::SUCESS) {
            $dao_response_data = $dao_response->getData();
            $fila_details_array = FilaEspecialidadeMapper::getFilaClientesDetailsArrayFromFilaClienteDetailsVMCollection($dao_response_data);
            $pagination = $dao_response->getPagination();
            $data = [
                'filas' => $fila_details_array,
                'pagination' => $pagination ? $pagination->toResponseArray() : null
            ];
            $service_response->setData($data);
        }
        return $service_response;
    }
   
}