<?php
namespace App\Classes\Modules\Transactions\Processors;

use App\Classes\Modules\Transactions\Services\CreatesShippingCostTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionDetailObject;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Models\Order;

class CreateShippingCostTransactionProcessor
{
    /** @var CreatesShippingCostTransaction */
    private $createsShippingCostTransaction;

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

    public function __construct(CreatesShippingCostTransaction $createsShippingCostTransaction, GeneratesTransactionBillNumber $generatesTransactionBillNumber)
    {
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
        $this->createsShippingCostTransaction = $createsShippingCostTransaction;
    }


    /**
     * @param Order $order
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Order $order,  int $quantity, float $totalCBM, float $totalShippingCost)
    {
        $billNumber = $this->generatesTransactionBillNumber->execute('SHP-');

        $transactionObject = new TransactionObject(
            $billNumber,
            TransactionType::SHIPPING_COST,
            1,
            $order->companyModule->company_id,
            1,
            PaymentMethodType::BA,
            $totalShippingCost,
            $totalShippingCost,
            1,
            1,
            0,
            0,
            0,
            null,
            ApprovalStatus::PENDING_SUBMISSION
        );

        $transactionDetailObject = new TransactionDetailObject(
            TransactionType::SHIPPING_COST,
            'Shipping Cost',
            $quantity,
            $totalShippingCost/$totalCBM
        );

        $shippingCostTransaction = $this->createsShippingCostTransaction->execute($order, $transactionObject, $transactionDetailObject);

    }
}
