<?php

namespace App\Classes\Modules\Wallets\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\Wallets\Services\CreatesWallet;
use App\Classes\Modules\Wallets\Services\GeneratesWalletCode;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Billplzs\Services\CreatesBillplzBill;
use App\Classes\Modules\Transactions\Services\CreatesTransactionableTransaction;
use App\Classes\Modules\Wallets\Processors\CreateWalletTopUpTransactionProcessor;
use App\Http\Resources\WalletTransactionResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class TopUpWalletLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'TopUp into Company Wallet',
            'message' => 'You have successfully created a topup request for company\'s wallet'
        ];
    }


    /** @var FetchesCompanyModule  */
    private $fetchesCompanyModule;

    /** @var GeneratesWalletCode */
    private $generatesWalletCode;

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

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

    /** @var CreatesBillplzBill */
    private $createsBillplzBill;

    /** @var CreatesTransactionableTransaction */
    private $createsTransactionableTransaction;

    /** @var CreateWalletTopUpTransactionProcessor */
    private $createWalletTopUpTransactionProcessor;


    /**
     * TopUpWalletLogic constructor.
     * @param FetchesCompanyModule $fetchesCompanyModule
     * @param GeneratesWalletCode $generatesWalletCode
     * @param CreatesWallet $createsWallet
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     * @param CreatesBillplzBill $createsBillplzBill
     * @param CreatesTransaction $createsTransaction
     * @param CreateWalletTopUpTransactionProcessor $createWalletTopUpTransactionProcessor
     */
    public function __construct(FetchesCompanyModule $fetchesCompanyModule, GeneratesWalletCode $generatesWalletCode, CreatesWallet $createsWallet, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesBillplzBill $createsBillplzBill, CreatesTransactionableTransaction $createsTransactionableTransaction, CreateWalletTopUpTransactionProcessor $createWalletTopUpTransactionProcessor)
    {
        $this->fetchesCompanyModule = $fetchesCompanyModule;
        $this->generatesWalletCode = $generatesWalletCode;
        $this->createsWallet = $createsWallet;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
        $this->createsBillplzBill = $createsBillplzBill;
        $this->createsTransactionableTransaction = $createsTransactionableTransaction;
        $this->createWalletTopUpTransactionProcessor = $createWalletTopUpTransactionProcessor;
    }

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

        $transaction = $this->createWalletTopUpTransactionProcessor->execute($companyModule, $amount, $request->input('bank_code'));

        return $this->resourceResponse(new WalletTransactionResource($transaction));
    }
}
