<?php

namespace App\Classes\Modules\Wallets\ControllersLogic;

use App\Classes\Exceptions\MalformedRequestException;
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\Wallets\Services\CreatesWallet;
use App\Classes\Modules\Wallets\Services\CreatesWalletTransaction;
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\CreatesTransaction;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\WalletResource;
use App\Http\Resources\WalletTransactionResource;
use App\Models\Wallet;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

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 FetchesCompany  */
    private $fetchesCompany;

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

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

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

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

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


    /**
     * TopUpWalletLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param GeneratesWalletCode $generatesWalletCode
     * @param CreatesWallet $createsWallet
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     * @param CreatesBillplzBill $createsBillplzBill
     * @param CreatesTransaction $createsTransaction
     */
    public function __construct(FetchesCompany $fetchesCompany, GeneratesWalletCode $generatesWalletCode, CreatesWallet $createsWallet, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesBillplzBill $createsBillplzBill, CreatesTransaction $createsTransaction)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->generatesWalletCode = $generatesWalletCode;
        $this->createsWallet = $createsWallet;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
        $this->createsBillplzBill = $createsBillplzBill;
        $this->createsTransaction = $createsTransaction;
    }

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

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

        if (!$wallet) {
            $object = new WalletObject($company->id, 1, $this->generatesWalletCode->execute());
            /** @var Wallet $wallet */
            $wallet = $this->createsWallet->execute($object, $company);
        }

        $user = $company->employees()->first();
        $billNumber = $this->generatesTransactionBillNumber->execute('TOPUP-');

        if($amount < 0) {
            throw new MalformedRequestException('Top up credit value must be greater than zero.');
        }

        $billPlzBill = $this->createsBillplzBill->execute($company->name, $user->email, 'This payment is credit topup for company ref. ' . $company->reference, $amount, $billNumber, $request->input('bank_code'), true);

        $transaction_object = new TransactionObject($billNumber, TransactionType::TOP_UP, 1, $company->id, 1, PaymentMethodType::PAYMENT_GATEWAY, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, [], $billPlzBill->id);

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

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