<?php

namespace App\DAO;

use App\DAO\DAO;
use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;

class ProfissionalDAO extends DAO {
	
	public function getIdProfissionalByMedicoId(int $idMedico):DAOResponse {
		$dao_response = new DAOResponse();
		
		$sql =<<<SQL
			select id_profissional from medico where id = :id_medico:
		SQL;
		
		$binds = ['id_medico' => $idMedico];
		
		$conn = db_connect();
		$result = $conn->query($sql,$binds)->getResultArray();
		$errors = $conn->error();
		
		if($errors['code']!=0) {
			$dao_response->setStatus(DAOResponseInterface::DATABASE_CONNECTION_ERROR);
			$dao_response->setErrors($errors);
		} else {
			if(count($result) > 0) {
				$dao_response->setStatus(DAOResponseInterface::SUCESS);
				$dao_response->setData($result);				
			}else {
				$dao_response->setStatus(DAOResponseInterface::RECORD_NOT_FOUND);
				$dao_response->setData([]);
			}
		}
		
		return $dao_response;

	}
	
	public function getIdProfissionalByAssistenteId(int $idAssistente):DAOResponse {
		$dao_response = new DAOResponse();
		
		$sql =<<<SQL
			select id_profissional from assistente where id = :id_assistente:
		SQL;
		
		$binds = ['id_assistente' => $idAssistente];
		
		$conn = db_connect();
		$result = $conn->query($sql,$binds)->getResultArray();
		$errors = $conn->error();
		
		if($errors['code']!=0) {
			$dao_response->setStatus(DAOResponseInterface::DATABASE_CONNECTION_ERROR);
			$dao_response->setErrors($errors);
		} else {
			if(count($result) > 0) {
				$dao_response->setStatus(DAOResponseInterface::SUCESS);
				$dao_response->setData($result);
			}else {
				$dao_response->setStatus(DAOResponseInterface::RECORD_NOT_FOUND);
				$dao_response->setData( [ ] );
			}
		}
		
		return $dao_response;
		
	}
	
	public function getIdProfissionalByAdministrativoId(int $idAdministrativo):DAOResponse {
		$dao_response = new DAOResponse();
		
		$sql =<<<SQL
			select id_profissional from administrativo where id = :id_administrativo:
		SQL;
		
		$binds = ['id_administrativo' => $idAdministrativo];
		
		$conn = db_connect();
		$result = $conn->query($sql,$binds)->getResultArray();
		$errors = $conn->error();
		
		if($errors['code']!=0) {
			$dao_response->setStatus(DAOResponseInterface::DATABASE_CONNECTION_ERROR);
			$dao_response->setErrors($errors);
		} else {
			if(count($result) > 0) {
				$dao_response->setStatus(DAOResponseInterface::SUCESS);
				$dao_response->setData($result);
			}else {
				$dao_response->setStatus(DAOResponseInterface::RECORD_NOT_FOUND);
				$dao_response->setData([]);
			}
		}
		
		return $dao_response;
		
	}
	
	public function getIdProfissionalByUsuarioId(int $idUsuario):DAOResponse {
		$dao_response = new DAOResponse();
		
		$sql =<<<SQL
			select p.id as id_profissional 
			from usuario u, profissional p
			where u.id = :id_usuario: and u.id_pessoa = p.id_pessoa 
		SQL;
		
		$binds = ['id_usuario' => $idUsuario];
		
		$conn = db_connect();
		$result = $conn->query($sql,$binds)->getResultArray();
		$errors = $conn->error();
		
		if($errors['code']!=0) {
			$dao_response->setStatus(DAOResponseInterface::DATABASE_CONNECTION_ERROR);
			$dao_response->setErrors($errors);
		} else {
			if(count($result) > 0) {
				$dao_response->setStatus(DAOResponseInterface::SUCESS);
				$dao_response->setData($result);
			}else {
				$dao_response->setStatus(DAOResponseInterface::RECORD_NOT_FOUND);
				$dao_response->setData([]);
			}
		}
		
		return $dao_response;
	}

}

