<?php

namespace App\Classes\Modules\Vouchers\Processors\Voucherify;

use App\Classes\Modules\Vouchers\DataTransferObjects\UpdateVoucherifyOrderObject;
use App\Classes\Modules\Vouchers\Services\Voucherify\UpdatesVoucherifyOrder;
use App\Models\Transaction;
use Illuminate\Support\Facades\Log;

class TransactionToVoucherifyProcessor
{
    /** @var UpdatesVoucherifyOrder */
    private $updatesVoucherifyOrder;

    /**
     * TransactionToVoucherifyProcessor constructor.
     * @param UpdatesVoucherifyOrder $updatesVoucherifyOrder
    */
    public function __construct(UpdatesVoucherifyOrder $updatesVoucherifyOrder)
    {
        $this->updatesVoucherifyOrder = $updatesVoucherifyOrder;
    }


    /**
     * @param Transaction $transaction
     * @param string $status
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \Voucherify\ClientException
     */
    public function execute(Transaction $transaction, string $status)
    {
        try{
            $voucherify_entity = $transaction->voucherifyEntities()->first();
            if($voucherify_entity){
                $updateVoucherifyOrderObject = new UpdateVoucherifyOrderObject($voucherify_entity->voucherify_entity_id, $status);
                $this->updatesVoucherifyOrder->execute($updateVoucherifyOrderObject);
            }
        } catch (\Exception $e) {
            Log::error($e);
        }
    }
}
