<?php

namespace App\Classes\Modules\Wallets\Processors;

use App\Models\Wallet;
use App\Models\Company;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\Modules\Wallets\Services\CreatesWallet;
use App\Classes\Modules\Wallets\Services\UpdatesWallet;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\Modules\Wallets\Services\GeneratesWalletCode;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;

class CreditWalletProcessor
{
    /** @var GeneratesWalletCode */
    private $generatesWalletCode;

    /** @var CreatesWallet  */
    private $createsWallet;

    /** @var GeneratesTransactionBillNumber  */
    private $generatesTransactionBillNumber;

    /** @var CreatesTransaction */
    private $createsTransaction;

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

    /**
     * CreateWalletLogic constructor.
     * @param GeneratesWalletCode $generatesWalletCode
     * @param CreatesWallet $createsWallet
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     * @param CreatesTransaction $createsTransaction
     * @param UpdatesWallet $updatesWallet
     */
    public function __construct(
        GeneratesWalletCode $generatesWalletCode,
        CreatesWallet $createsWallet,
        GeneratesTransactionBillNumber $generatesTransactionBillNumber,
        CreatesTransaction $createsTransaction,
        UpdatesWallet $updatesWallet
    )
    {
        $this->generatesWalletCode = $generatesWalletCode;
        $this->createsWallet = $createsWallet;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
        $this->createsTransaction = $createsTransaction;
        $this->updatesWallet = $updatesWallet;
    }


    /**
     * @param Company $company
     * @param int $transactionType
     * @param float $amount
     * @param string $reference
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Company $company, int $transactionType, float $amount, string $reference)
    {
        if (!$company->wallets()->first()) {
            $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute());
            $this->createsWallet->execute($object, $company);
        }

        /** @var Wallet $wallet */
        $wallet = $company->wallets()->first();

        $billNumber = $this->generatesTransactionBillNumber->execute($transactionType === 2 ? 'DEBIT-NOTE-' : 'CREDIT-NOTE-');
        
        $transaction_object = new TransactionObject($billNumber, $transactionType === 2 ? TransactionType::DEBIT_NOTE : TransactionType::CREDIT_NOTE, 1,  $wallet->owner->id,  1, PaymentMethodType::CASH,  $amount,  $amount,  1,  1,  1,  0,  0,  null,  ApprovalStatus::APPROVED,  [], $reference);
        $transaction = $this->createsTransaction->execute($wallet, $transaction_object);
        
        $updateWalletAmount = $transactionType === 2 ? ($wallet->amount - $transaction->amount) : ($wallet->amount + $transaction->amount);

        $walletObject = new WalletObject($wallet->owner->id, $wallet->currency_id, $wallet->code, $updateWalletAmount);

        $wallet = $this->updatesWallet->execute($wallet, $walletObject);

        return $wallet;
    }
}
