<?php

namespace App\Classes\Modules\Wallets\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject;
use App\Classes\Modules\Wallets\Services\ListsWallet;
use App\Classes\Modules\Wallets\Services\FetchesWallet;
use App\Classes\ValueObjects\Constants\TransactionType;

use App\Classes\Modules\Wallets\Standards\Rules\CanWithdrawWallet;
use App\Http\Resources\WalletResource;
use App\Classes\Modules\Transactions\Processors\CreateWalletTransactionProcessor;

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

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

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

   /** @var CanWithdrawWallet */
   private $canWithdrawWallet;

    /** @var CreateWalletTransactionProcessor */
    private $createWalletTransactionProcessor;

    /**
     * CreateWalletLogic constructor.
     * @param CreatesWallet $createsWallet
     * @param GeneratesWalletCode $generatesWalletCode
     * @param CanCreateCompanyWallet $canCreateCompanyWallet
     */
    public function __construct(CanWithdrawWallet $canWithdrawWallet, FetchesWallet $fetchesWallet, CreateWalletTransactionProcessor $createWalletTransactionProcessor)
    {
        $this->canWithdrawWallet = $canWithdrawWallet;
        $this->fetchesWallet = $fetchesWallet;
        $this->createWalletTransactionProcessor = $createWalletTransactionProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $wallet = $this->fetchesWallet->execute(['id' => $request->input('wallet_id')]);

        $walletOject = new WalletObject( $wallet->company->id,  $wallet->currency_id,  $wallet->code, $request->input('amount'));

        $this->canWithdrawWallet->passes($walletOject);

        $transaction = $this->createWalletTransactionProcessor->execute($wallet, $walletOject, TransactionType::WITHDRAW);

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