<?php

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

use App\Classes\Modules\Vouchers\DataTransferObjects\RedeemVoucherifyVoucherObject;

class RedeemsVoucherifyVoucher
{

    /** @var VoucherifyClient */
    private $voucherifyClient;


    /**
     * RedeemsVoucherifyVoucher constructor.
     *
     */
    public function __construct()
    {
        $this->voucherifyClient = createVoucherifyClient();
    }

     /**
     * @param RedeemVoucherifyVoucherObject $redeemVoucherifyVoucherObject
     * @return null|object
     * @throws \Voucherify\ClientException
     */
    public function execute(RedeemVoucherifyVoucherObject $redeemVoucherifyVoucherObject)
    {
        try {
            $result = $this->voucherifyClient->redemptions->redeem($redeemVoucherifyVoucherObject->getPromoCode(), [
                "customer" => [
                    "source_id" => $redeemVoucherifyVoucherObject->getEmployee()->id,
                    "name" => $redeemVoucherifyVoucherObject->getEmployee()->name,
                    "email" => $redeemVoucherifyVoucherObject->getEmployee()->email,
                    "metadata" => [
                        "exchange_company_id" => $redeemVoucherifyVoucherObject->getCompanyId(),
                        "exchange_user_id" => $redeemVoucherifyVoucherObject->getEmployee()->id
                    ]
                ],
                "order" => [
                    "source_id" => $redeemVoucherifyVoucherObject->getTransactionId(),
                    "amount" => $redeemVoucherifyVoucherObject->getAmount() * 100  //converting it to cents
                ]
            ]);
            return $result;
        } catch (\Voucherify\ClientException $e) {
            throw $e;
        }
    }
}
