<?php

namespace App\Classes\Modules\Transactions\Services;

use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Models\Transaction;
use App\Classes\Modules\PerfexCRM\Processors\TransactionToPerfexCRMV2Processor;

class UpdatesTransactionStatus extends AbstractUpdateRecord
{
    /** @var TransactionToPerfexCRMV2Processor */
    private $transactionToPerfexCRMV2Processor;

    /**
     * UpdatesTransactionStatus constructor.
     * @param TransactionToPerfexCRMProcessor $transactionToPerfexCRMV2Processor
     */
    public function __construct(TransactionToPerfexCRMV2Processor $transactionToPerfexCRMV2Processor)
    {
        $this->transactionToPerfexCRMV2Processor = $transactionToPerfexCRMV2Processor;
    }

    /**
     * @param Transaction $model
     * @param int $status
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Transaction $model, int $status)
    {
        $transaction = clone($model);
        $model->status = $status;
        $result = $this->handler($model);

        if(config('perfexcrm.is_enabled') == 'true'){
            $this->transactionToPerfexCRMV2Processor->execute($transaction, $status);
        }
        return $result;
    }
}
