<?php

namespace App\DAO;

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

class ProfissionalClinicaDAO extends DAO {

	public function updateProfissionalClinica(int $idProfissional, int $clinica): DAOResponse {

		$dao_response = new DAOResponse();

		$sql = <<<SQL
			insert into profissional_clinica (id_profissional, id_pessoa_juridica) values
			(:id_profissional:,:id_clinica:)
		SQL;
		$binds = [ 
			'id_profissional' => $idProfissional,
			'id_clinica' => $clinica
		];

		$conn = db_connect();
		$result = $conn->query( $sql, $binds )->numRows;
		$errors = $conn->error();

		if ($errors ['code'] != 0) {
			$dao_response->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$dao_response->setErrors( $errors );
			$dao_response->setData( [ ] );
		} else {
			$dao_response->setStatus( DAOResponseInterface::SUCESS );
			$dao_response->setData( [ ] );
		}

		return $dao_response;

	}

	public function excluirProfissionalClinica(int $idProfissional, int $clinica): DAOResponse {

		$dao_response = new DAOResponse();

		$sql_pj = <<<SQL
			select id_pessoa_juridica from unidade un where un.id = :id_clinica:
		SQL;

		$binds_pj = [ 
			'id_clinica' => $clinica
		];

		$conn_pj = db_connect();
		$result_pj = $conn_pj->query( $sql_pj, $binds_pj )
			->getResultArray();
		$errors_pj = $conn_pj->error();

		if ($errors_pj ['code'] != 0) {
			$dao_response->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$dao_response->setErrors( $errors_pj );
			$dao_response->setData( [ ] );
			return $dao_response;
		}

		$idPj = $result_pj [0] ['id_pessoa_juridica'];

		$sql = <<<SQL
			delete from profissional_clinica
			where id_profissional = :id_profissional: and id_pessoa_juridica = :id_pj:
		SQL;
		$binds = [ 
			'id_profissional' => $idProfissional,
			'id_pj' => $idPj
		];

		$conn = db_connect();
		$result = $conn->query( $sql, $binds )->numRows;
		$errors = $conn->error();

		if ($errors ['code'] != 0) {
			$dao_response->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$dao_response->setErrors( $errors );
			$dao_response->setData( [ ] );
		} else {
			$affectrows = $conn->affectedRows();
			$dao_response->setStatus( DAOResponseInterface::SUCESS );
			$dao_response->setData( [ 
				'registros' => $affectrows
			] );
		}

		return $dao_response;

	}

	public function incluirProfissionalClinica(int $idProfissional, int $clinica): DAOResponse {

		$dao_response = new DAOResponse();

		$sql_pj = <<<SQL
			select id_pessoa_juridica from unidade un where un.id = :id_clinica:
		SQL;

		$binds_pj = [ 
			'id_clinica' => $clinica
		];

		$conn_pj = db_connect();
		$result_pj = $conn_pj->query( $sql_pj, $binds_pj )
			->getResultArray();
		$errors_pj = $conn_pj->error();

		if ($errors_pj ['code'] != 0) {
			$dao_response->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$dao_response->setErrors( $errors_pj );
			$dao_response->setData( [ ] );
			return $dao_response;
		}

		$idPj = $result_pj [0] ['id_pessoa_juridica'];

		$sql = <<<SQL
			insert into profissional_clinica 
			(id_profissional,id_pessoa_juridica) values
			(:id_profissional:,:id_pj:)
		SQL;
		$binds = [ 
			'id_profissional' => $idProfissional,
			'id_pj' => intval( $idPj )
		];

		$conn = db_connect();
		$result = $conn->query( $sql, $binds )->numRows;
		$errors = $conn->error();

		if ($errors ['code'] != 0) {
			$dao_response->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$dao_response->setErrors( $errors );
			$dao_response->setData( [ ] );
		} else {
			$affectrows = $conn->affectedRows();
			$dao_response->setStatus( DAOResponseInterface::SUCESS );
			$dao_response->setData( [ 
				'registros' => $affectrows
			] );
		}

		return $dao_response;

	}

	public function getClinicasByProfissionalId(int $idProfissional): DAOResponse {

		$dao_response = new DAOResponse();

		$sql = <<<SQL
			select distinct 
				pc.id_pessoa_juridica , un.id as is_unidade
			from 
				profissional_clinica pc, unidade un
			where 
				pc.id_profissional = :id_profissional: 
				and pc.id_pessoa_juridica = un.id_pessoa_juridica 
		SQL;
		$binds = [ 
			'id_profissional' => $idProfissional
		];

		$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 );
			$dao_response->setData( [ ] );
		} else {
			if (count( $result ) == 0) {
				$dao_response->setStatus( DAOResponseInterface::RECORD_NOT_FOUND );
				$dao_response->setData( [ ] );
			} else {
				$dao_response->setStatus( DAOResponseInterface::SUCESS );
				$dao_response->setData( $result );
			}
		}

		return $dao_response;

	}

	public function getProfissionalCadastradoClinicaId(int $idProfissional, $clinica): DAOResponse {

		$dao_response = new DAOResponse();

		$sql = <<<SQL
			select id_pessoa_juridica from profissional_clinica
			where id_profissional = :id_profissional: and id_pessoa_juridica = :id_clinica:
		SQL;
		$binds = [ 
			'id_profissional' => $idProfissional,
			'id_clinica' => $clinica
		];

		$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 );
			$dao_response->setData( [ ] );
		} else {
			if (count( $result ) == 0) {
				$dao_response->setStatus( DAOResponseInterface::RECORD_NOT_FOUND );
				$dao_response->setData( [ ] );
			} else {
				$dao_response->setStatus( DAOResponseInterface::SUCESS );
				$dao_response->setData( $result );
			}
		}

		return $dao_response;

	}

	public function getProfissionaisByUnidadeId(int $idUnidade): DAOResponse {

		$dao_response = new DAOResponse();

		$sql = <<<SQL
			select
				distinct pr.id , pf.nome , u.id as id_usuario , up.id_perfil , p.descricao as perfil
			from
				profissional_clinica pc, profissional pr, usuario u,
				pessoa_fisica pf , unidade un, usuario_perfil up , perfil p
			where
				un.id = :id_unidade:
				and pc.id_pessoa_juridica = un.id_pessoa_juridica
				and pc.id_profissional = pr.id
				and pr.id_pessoa = pf.id_pessoa
				and pf.id_pessoa = u.id_pessoa
				and u.ativo  = 1
				and u.id = up.id_usuario
				and up.id_perfil in (2,3)
				and up.id_perfil = p.id
			order by pf.nome asc
		SQL;

		$binds = [ 
			'id_unidade' => $idUnidade
		];

		$conn = db_connect();
		$result = $conn->query( $sql, $binds )
			->getResultArray();
		$errors = $conn->error();

		if ($errors ['code'] != 0) {
			$dao_response->setErrors( $errors );
			$dao_response->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
		} else {
			$dao_response->setData( [ 
				'profissionais' => $result
			] );
			$dao_response->setStatus( DAOResponseInterface::SUCESS );
		}
		return $dao_response;

	}

}

