<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\DeletesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Transaction;
use App\Classes\Modules\Bookings\ControllersLogic\UpdateBookingAmountLogic;
use App\Classes\Modules\Bookings\Services\CalculatesBookingRefundAmount;
use Illuminate\Support\Facades\Log;
use App\Classes\Modules\Bookings\Services\CalculatesBookingPaidAmount;

class DeleteRefundTransactionLogic extends AbstractControllerLogic
{

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

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

    /** @var DeletesTransaction */
    private $deletesTransaction;

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

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

    /** @var CalculatesBookingPaidAmount */
    private $calculatesBookingPaidAmount;

    /** @var UpdateBookingAmountLogic */
    private $updateBookingAmountLogic;

    /**
     * CreatePaymentVerificationDocumentLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param DeletesTransaction $deletesTransaction
     * @param CalculatesBookingRefundAmount $calculatesBookingRefundAmount
     * @param UpdateBookingAmountLogic $updateBookingAmountLogic
     * @param calculatesBookingPaidAmount $calculatesBookingPaidAmount
     */
    public function __construct(FetchesTransaction $fetchesTransaction, DeletesTransaction $deletesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, CalculatesBookingRefundAmount $calculatesBookingRefundAmount, UpdateBookingAmountLogic $updateBookingAmountLogic, CalculatesBookingPaidAmount $calculatesBookingPaidAmount)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->deletesTransaction = $deletesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->calculatesBookingRefundAmount = $calculatesBookingRefundAmount;
        $this->updateBookingAmountLogic = $updateBookingAmountLogic;
        $this->calculatesBookingPaidAmount = $calculatesBookingPaidAmount;
    }

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

        // Update payment_transaction status 
        $payment_transaction = $transaction->owner;
        $this->updatesTransactionStatus->execute($payment_transaction, ApprovalStatus::APPROVED);

        // delete wallet top up transaction 
        $booking = $transaction->owner->owner;
        Transaction::where('type', TransactionType::CREDIT_NOTE)
            ->where('amount', $transaction->amount)
            ->where('payment_reference', 'like', '%' . $booking->marking . '%')
            ->delete();

        // update back the latest booking amount
        $request['fix_amount'] = $this->calculatesBookingPaidAmount->execute($booking);
        $request->route()->setParameter('id', $booking->id);
        $this->updateBookingAmountLogic->execute($request);

        // if have SUPPLIER_REFUND transaction
        $bookingInWhiteForm = $payment_transaction->transactions()->bills()->first();
        if ($bookingInWhiteForm) {

            $whiteFormTransaction = Transaction::where('type', TransactionType::SUPPLIER_REFUND)
                ->where('payment_reference', $transaction->payment_reference)
                ->first();

            // Log::info($whiteFormTransaction->id);

            $whiteFormTransaction->delete();
        }

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