<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;


use App\Classes\Modules\Documents\Services\ApprovesDocument;
use App\Classes\Modules\Documents\Services\FetchesDocument;
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\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
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, ApprovesDocument $approvesDocument, RejectsDocument $rejectsDocument, UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor, UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->approvesDocument = $approvesDocument;
        $this->rejectsDocument = $rejectsDocument;
        $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor;
        $this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor;
    }

    /**
     * @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 UpdateDoFromVTPortalProcessor */
    private $updateDoFromVTPortalProcessor;

    /** @var UpdateDoFromYDPortalProcessor */
    private $updateDoFromYDPortalProcessor ;

    /**
     * @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()->first()) : $this->rejectsDocument->execute($transaction->documents()->first());

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

        if($transaction->status === ApprovalStatus::APPROVED){
            $invoice = $transaction->owner;
            $packingList = $invoice->owner;

            if(($invoice->amount - $transaction->amount) < 0.01) {
                $this->updatesTransactionStatus->execute($invoice, ApprovalStatus::COMPLETED);

                $packingList->status = ApprovalStatus::APPROVED;
                $packingList->save();

                if(app()->environment('production')){
                    $this->updateDoFromVTPortalProcessor->execute($packingList);
                    $this->updateDoFromYDPortalProcessor->execute($packingList);
                }
            }

        }


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

}
