<?php

namespace App\DAO;

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

class ClienteDAO extends DAO {

	public function getClienteIdByUsuarioId(int $idusuario): DAOResponse {

		$daoResponse = new DAOResponse();

		$sql = <<<SQL
			SELECT 
				c.id , c.id_pessoa , c.aceite_termo , c.criado_em  
			FROM
				usuario u, cliente c
			WHERE
				u.id = :usuario_id:
				and u.id_pessoa = c.id_pessoa 
		SQL;
		$binds = [ 
			'usuario_id' => $idusuario
		];

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

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

			if (count( $result ) == 0) {
				$daoResponse->setStatus( DAOResponseInterface::RECORD_NOT_FOUND );
				$daoResponse->setData( [ ] );
			} else {
				$daoResponse->setStatus( DAOResponseInterface::SUCESS );
				$daoResponse->setData( $result[0] );
			}
		}

		return $daoResponse;

	}

	public function setAceitoTermos(int $idcliente): DAOResponse {

		$sql = <<<SQL
			update cliente
			set aceite_termo = 1
			where  id  = :cliente_id:
		SQL;

		$binds = [ 
			'cliente_id' => $idcliente
		];

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

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

		return $daoResponse;

	}

}

