<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Http\Resources\GroupResource;
use App\Classes\Modules\Transactions\Services\FetchesGroup;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateGroupTransferFeeLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Update Group Transfer Fee',
            'message' => 'You have successfully updated transfer fee for this Group Transaction'
        ];
    }

    /** @var FetchesGroup */
    private $fetchesGroup;

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

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

    /**
     * UpdateGroupTransferFeeLogic constructor.
     * @param FetchesGroup $fetchesGroup
     * @param CreatesTransaction $createsTransaction
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     */
    public function __construct(FetchesGroup $fetchesGroup, CreatesTransaction $createsTransaction, GeneratesTransactionBillNumber $generatesTransactionBillNumber)
    {
        $this->fetchesGroup = $fetchesGroup;
        $this->createsTransaction = $createsTransaction;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $group = $this->fetchesGroup->execute(['id' => $request->route('id')]);
        $supplier = $group->issuerCompany;
        $fee = $request->input('fee');

        $transfer_fee = $group->morphTransactions()->where('type', TransactionType::TRANSFER_FEE)->first();

        if ($transfer_fee) {
            $transfer_fee->amount = $fee;
            $transfer_fee->original_amount = $fee;
            $transfer_fee->save();
        } else {
            $transferFeeNumber = $this->generatesTransactionBillNumber->execute('TRFR-');
            $object = new TransactionObject($transferFeeNumber, TransactionType::TRANSFER_FEE, 1, $supplier->id,
                $supplier->banks()->where('default', true)->first()->id, PaymentMethodType::CASH,
                $fee, $fee, $group->original_currency_id, $group->original_currency_id,
                1, 0, 0, null, ApprovalStatus::APPROVED);

            $model = new Transaction();
            $model->bill_no = $object->getBillNo();
            $model->type = $object->getTransactionType();
            $model->issuer = $object->getIssuer();
            $model->receiver = $object->getReceiver();
            $model->recipient_bank_account_id = $object->getRecipientBankAccountId();
            $model->payment_method = $object->getPaymentMethod();
            $model->amount = $object->getAmount();
            $model->original_amount = $object->getOriginalAmount();
            $model->currency_id = $object->getCurrencyId();
            $model->original_currency_id = $object->getOriginalCurrencyId();
            $model->currency_rate = $object->getCurrencyRate();
            $model->tax = $object->getTax();
            $model->service_charge = $object->getServiceCharge();
            $model->expires_on = $object->getExpiresOn();
            $model->status = $object->getStatus();
            $model->payment_reference = $object->getPaymentReference();

            $group->morphTransactions()->save($model);
        }

        return $this->resourceResponse(new GroupResource($group));
    }

}
