<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Billplzs\Processors\CallbackBillplzProcessor;
use App\Classes\Modules\Documents\Services\ApprovesDocument;
use App\Classes\Modules\Documents\Services\RejectsDocument;
use App\Classes\Modules\Orders\Processors\UpdateDoFromVTPortalProcessor;
use App\Classes\Modules\Orders\Processors\UpdateDoFromYDPortalProcessor;
use App\Classes\Modules\Transactions\Processors\CreatePaymentTransactionProcessor;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ApprovePaymentTransactionLogic extends AbstractControllerLogic
{
    /**
     * ApprovePaymentTransactionLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param ApprovesDocument $approvesDocument
     * @param RejectsDocument $rejectsDocument
     * @param UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor
     * @param UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor
     * @param CreatePaymentTransactionProcessor $createPaymentTransactionProcessor
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, CallbackBillplzProcessor $callbackBillplzProcessor)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->approvesDocument = $approvesDocument;
        $this->rejectsDocument = $rejectsDocument;
        $this->callbackBillplzProcessor = $callbackBillplzProcessor;
    }

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Payment Status',
            'message' => 'You have successfully updated the payment status'
        ];
    }

    /** @var FetchesTransaction */
    private $fetchesTransaction;

    /** @var UpdatesTransactionStatus */
    private $updatesTransactionStatus;

    /** @var ApprovesDocument */
    private $approvesDocument;

    /** @var RejectsDocument */
    private $rejectsDocument;

    /** @var CallbackBillplzProcessor */
    private $callbackBillplzProcessor;

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\InternalServerErrorException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function logic(Request $request) : JsonResponse
    {

        $status = $request->route('status');

        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('transaction_id')]);

        $status === 'approve' ? $this->approvesDocument->execute($transaction->documents()->where('status', ApprovalStatus::PENDING_VERIFICATION)->first()) : $this->rejectsDocument->execute($transaction->documents()->where('status', ApprovalStatus::PENDING_VERIFICATION)->first());

        $this->updatesTransactionStatus->execute($transaction, $status === 'approve' ? ApprovalStatus::APPROVED : ApprovalStatus::REJECTED);

        if ($status === 'approve') {
            $this->callbackBillplzProcessor->execute($transaction, ApprovalStatus::APPROVED);
        }

        return $this->response([]);
    }

}
