<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\Modules\Documents\Services\DeletesDocument;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

use App\Classes\Modules\Wallets\Processors\CreditWalletProcessor;
use App\Classes\Modules\Bookings\Services\CalculatesBookingPayableAmount;
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount;


class UpdateRefundTransactionStatusLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated Transaction',
            'message' => 'You have successfully updated a transaction'
        ];
    }

    /** @var FetchesCompany */
    private $fetchesCompany;

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

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

    /** @var DeletesDocument */
    private $deletesDocument;

    /** @var CreditWalletProcessor */
    private $creditWalletProcessor;

    /** @var CalculatesBookingPayableAmount */
    private $calculatesBookingPayableAmount;

    /** @var CalculatesBookingRefundAmount */
    private $calculatesBookingRefundAmount;

    /**
     * CreatePaymentVerificationDocumentLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param DeletesDocument $deletesDocument
     * @param CreditWalletProcessor $creditWalletProcessor
     * @param CalculatesBookingPayableAmount $calculatesBookingPayableAmount
     * @param CalculatesBookingRefundAmount $calculatesBookingRefundAmount
     */
    public function __construct(FetchesCompany $fetchesCompany, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, DeletesDocument $deletesDocument, CreditWalletProcessor $creditWalletProcessor, CalculatesBookingPayableAmount $calculatesBookingPayableAmount, CalculatesBookingRefundAmount $calculatesBookingRefundAmount)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->deletesDocument = $deletesDocument;
        $this->creditWalletProcessor = $creditWalletProcessor;
        $this->calculatesBookingPayableAmount = $calculatesBookingPayableAmount;
        $this->calculatesBookingRefundAmount = $calculatesBookingRefundAmount;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $refundTransaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);

        $refundTransaction = $this->updatesTransactionStatus->execute($refundTransaction, $request->route('status'));

        $paymentTransaction = $refundTransaction->owner;

        $booking = $paymentTransaction->owner;

        $reference = $refundTransaction->amount == $paymentTransaction->amount ? 'Fully Refund for Ref. ' . $booking->marking : 'Partially Refund for Ref. ' . $booking->marking;

        if ($refundTransaction->status == ApprovalStatus::APPROVED) {
            $this->creditWalletProcessor->execute($booking->company, $refundTransaction->type, $refundTransaction->amount, $reference);
        }

        $paidAmount = $paymentTransaction->original_amount - $this->calculatesBookingRefundAmount->calculateRefundAmount($paymentTransaction, $booking->fix_currency_id);
        if (!$paidAmount > 0) {
            $this->updatesTransactionStatus->execute($paymentTransaction, ApprovalStatus::REFUNDED);
        }

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