<?php

namespace App\Classes\Modules\Wallets\ControllersLogic;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Resources\WalletResource;
use App\Classes\Modules\Wallets\Services\CreatesWallet;
use App\Classes\Modules\Wallets\Services\UpdatesWallet;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\Wallets\Services\GeneratesWalletCode;

use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Wallets\Processors\CreditWalletProcessor;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;

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


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

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

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

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

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

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

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

    /** @var CreditWalletProcessor */
    private $creditWalletProcessor;

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

    /**
     * @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')]);

        $reference = $request->input('reference');

        $type = $request->input('transaction_type');

        $wallet = $this->creditWalletProcessor->execute($companyModule, $type, $amount, $reference);

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