<?php

namespace App\Classes\Modules\Transactions\Services;

use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionDetailObject;
use App\Models\Order;
use App\Models\Transaction;
use App\Models\TransactionDetail;

class CreatesShippingCostTransaction extends AbstractUpdateRelationshipRecord
{
    /**
     * @param TransactionObject $object
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Order $order, TransactionObject $object, TransactionDetailObject $transactionDetailObject) {
        $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();

        $transaction = $this->handler($order->transactions(), $model);


        $model = new TransactionDetail();
        $model->reference = $transactionDetailObject->getReference();
        $model->name = $transactionDetailObject->getName();
        $model->quantity = $transactionDetailObject->getQuantity();
        $model->price = $transactionDetailObject->getPrice();
        $model->amount = $transactionDetailObject->getAmount();

        return $this->handler($transaction->transactionDetails(), $model);


    }
}
