<?php


namespace App\Classes\Modules\Wallets\Processors;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
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\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\CompanyModule;
use App\Models\Wallet;
use PhpOffice\PhpSpreadsheet\Calculation\Logical\Boolean;

class CreateWalletTopUpTransactionProcessor
{
    /** @var GeneratesWalletCode */
    private $generatesWalletCode;

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

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

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

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

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

    /**
     * @throws MalformedRequestException
     */
    public function execute(CompanyModule $companyModule, $amount, $bank_code, ?int $payment_method = PaymentMethodType::PAYMENT_GATEWAY, ?string $payment_reference = null, ?bool $groupTransaction = false)
    {
        /** @var Wallet $wallet */
        $wallet = $companyModule->wallets()->first();

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

        $user = $companyModule->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($companyModule->name, $user->email, 'This payment is credit topup for company ref. ' . $companyModule->reference, $amount, $billNumber, $bank_code, true);

        // IF $groupTransaction, transaction type is GROUP_PAYMENT
        // $transaction_object = new TransactionObject($billNumber, $groupTransaction? TransactionType::GROUP_PAYMENT: TransactionType::TOP_UP, 1, $companyModule->id, 1, PaymentMethodType::PAYMENT_GATEWAY, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, [], $billPlzBill->id);
        // no more group payment
        $transaction_object = new TransactionObject($billNumber, TransactionType::TOP_UP, 1, $companyModule->id, 1, $payment_method, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, [], $payment_method === PaymentMethodType::PAYMENT_GATEWAY ? $billPlzBill->id : $payment_reference);

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

        return $transaction;
    }
}
