<?php

namespace App\Classes\Modules\Wallets\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Wallets\Services\UpdatesWallet;

use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\WalletResource;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DebitWalletLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Debit into Company Wallet',
            'message' => 'You have successfully debit company wallet'
        ];
    }


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

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

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

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

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

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $amount = floatval(str_replace(',', '', $request->input('amount')));
        $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]);
        $reference = $request->input('reference');

        if (!$company->wallets()->first()) {
            $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute());
            $wallet = $this->createsWallet->execute($object, $company);
        }

        $wallet = $company->wallets()->first();

        $billNumber = $this->generatesTransactionBillNumber->execute('DEBIT-NOTE-');
        
        $transaction_object = new TransactionObject(
            $billNumber, 
            TransactionType::DEBIT_NOTE, 
            1, 
            $wallet->company->id,
            1, 
            PaymentMethodType::CASH,
            $amount, 
            $amount, 
            1,
            1, 
            1,
            0, 
            0, 
            null, 
            ApprovalStatus::APPROVED,
            [],
            $reference
        );

        $transaction = $this->createsTransaction->execute($wallet, $transaction_object);
        $updateWalletAmount = $wallet->amount - $transaction->amount;

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

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

        return $this->resourceResponse(new WalletResource($wallet));
    }
}
