<?php

namespace App\Classes\Modules\Transactions\Processors;

use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Wallets\Services\FetchesWallet;
use App\Classes\Modules\Wallets\Services\UpdatesWallet;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Models\Wallet;

class UpdateWalletTransactionProcessor
{
    /** @var CreatesWalletTransaction */
    private $createsWalletTransaction;

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

    /** @var ListWallet  */
    private $fetchesWallet;

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

    /** @var UpdatesWallet  */
    private $updatesWallet;

    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, UpdatesWallet $updatesWallet)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->updatesWallet = $updatesWallet;
    }


    /**
     * @param Booking $booking
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(int $transactionId, int $transactionStatus)
    {

        $transaction = $this->fetchesTransaction->execute(['id' => $transactionId]);
        $wallet = $transaction->wallet;

        switch($transaction->type){
            case TransactionType::TOP_UP:
                $updateWalletAmount = $transaction->amount + $wallet->amount;
                break;
            case TransactionType::WITHDRAW:
                $updateWalletAmount = $wallet->amount - $transaction->amount;
                break;
            default:
                break;
        }

        $walletOject = new WalletObject( $wallet->company->id,  $wallet->currency_id,  $wallet->code, $updateWalletAmount);

        $this->updatesTransactionStatus->execute($transaction, $transactionStatus);

        $this->updatesWallet->execute($wallet, $walletOject);

        return $wallet;
    }
}
