<?php

namespace App\DAO;

use App\DAO\DAO;
use App\DAO\DAOResponse\DAOResponse;
use App\DAO\DAOResponse\DAOResponseInterface;
use App\Utils\DateUtils;
use App\ViewModels\PagSeguro\PagSeguroNotificationCreditCard\PagSeguroChargeViewModel;
use App\ViewModels\PagSeguro\PagSeguroNotificationCreditCard\PagSeguroCustomerViewModel;

class PagSeguroNotificationDAO extends DAO {

	public function registerNotification(string $objresponse, string $referenceid, string $orderid, PagSeguroChargeViewModel $charge,
			PagSeguroCustomerViewModel $customer): DAOResponse {

		$daoResponse = new DAOResponse();

		$arr_phones = $customer->getPhones()
			->getPhones();

		$phones = '';

		for($i = 0; $i < count( $arr_phones ); $i++) {
			if ($i == 0) {
				$phones .= $arr_phones [$i]->getArea() . '-' . ($arr_phones [$i]->getNumber());
			} else {
				$phones .= ' , ' . $arr_phones [$i]->getArea() . '-' . ($arr_phones [$i]->getNumber());
			}
		}

		$sql = <<<SQL
			insert into pagseguro_response
				(webhook,reference_id, order_id, charge_id, charge_status, data_criacao, hora_criacao,
					charge_created_data, charge_created_hora, charge_paid_data, charge_paid_hora ,response,
					charge_reference_id, charge_payment_response_reference, charge_payment_method_type,
					charge_payment_method_installments, charge_amount_value, charge_amount_paid, charge_amount_total,
					charge_amount_refunded, customer_name, custumer_email, customer_phone, charge_holder_name) values
				(:webhook:,:idreferencia:,:id_order:,:id_charge:, :status_charge:, :data_c:,:hora_c:,
				:charge_data:,:charge_hora:,:charge_paid_data:, :charge_paid_hora:, :objresp:,
				:charge_reference_id:, :charge_payment_response_reference:, :charge_payment_method_type:,
				:charge_payment_method_installments:, :charge_amount_value:, :charge_amount_paid:, :charge_amount_total:,
				:charge_amount_refunded:, :customer_name:, :custumer_email:, :customer_phone:, :charge_holder_name:)
		SQL;

		$binds = [ 
			'webhook' => 'S',
			'idreferencia' => $referenceid,
			'id_order' => $orderid,
			'id_charge' => $charge->getId(),
			'status_charge' => $charge->getStatus(),
			'data_c' => DateUtils::today(),
			'hora_c' => DateUtils::now_time(),
			'charge_data' => NULL,
			'charge_hora' => NULL,
			'charge_paid_data' => NULL,
			'charge_paid_hora' => NULL,
			'objresp' => $objresponse,
			'charge_reference_id' => $charge->getReferenceId(),
			'charge_payment_response_reference' => $charge->getPaymentResponse()
				->getReference(),
			'charge_payment_method_type' => $charge->getPaymentMethod()
				->getType(),
			'charge_payment_method_installments' => $charge->getPaymentMethod()
				->getInstallments(),
			'charge_amount_value' => $charge->getAmount()
				->getValue(),
			'charge_amount_paid' => $charge->getAmount()
				->getSummary()
				->getPaid(),
			'charge_amount_total' => $charge->getAmount()
				->getSummary()
				->getTotal(),
			'charge_amount_refunded' => $charge->getAmount()
				->getSummary()
				->getRefunded(),
			'customer_name' => $customer->getName(),
			'custumer_email' => $customer->getEmail(),
			'customer_phone' => $phones,
			'charge_holder_name' => $charge->getPaymentMethod()
				->getCard()
				->getHolder()
				->getName()
		];

		$createat = $charge->getCreatedAt();
		$paidat = $charge->getPaidAt();
		$data_charge_create = "";
		$hora_charge_create = "";
		$data_paid_create = "";
		$hora_paid_create = "";
		if (strlen( trim( $createat ) > 10 )) {
			$chgrd_arr = explode( "T", $createat );
			$chgrh_arr = explode( ".", $chgrd_arr [1] );
			$data_charge_create = $chgrd_arr [0];
			$hora_charge_create = $chgrh_arr [0];
			$binds ['charge_data'] = $data_charge_create;
			$binds ['charge_hora'] = $hora_charge_create;
		}
		if (strlen( trim( $createat ) > 10 )) {
			$paidat_arr = explode( "T", $paidat );
			$paidh_arr = explode( ".", $paidat_arr [1] );
			$data_paid_create = $paidat_arr [0];
			$hora_paid_create = $paidh_arr [0];
			$binds ['charge_paid_data'] = $data_paid_create;
			$binds ['charge_paid_hora'] = $hora_paid_create;
		}

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

		if ($errors ['code'] != 0) {
			$daoResponse->setStatus( DAOResponseInterface::DATABASE_CONNECTION_ERROR );
			$daoResponse->setErrors( $errors );
			$daoResponse->setData( [ ] );
			log_message( "ERROR", json_encode( $errors ) );
		} else {
			$affectrows = $conn->affectedRows();
			if ($affectrows == 0) {
				$daoResponse->setStatus( DAOResponseInterface::INSUFFICIENT_DATA );
				$daoResponse->setData( [ ] );
			} else {
				$newid = $conn->insertID();
				$daoResponse->setStatus( DAOResponseInterface::SUCESS );
				$daoResponse->setData( [ 
					'id_response' => $newid
				] );
			}
		}

		return $daoResponse;

	}

}

