<?php

namespace App\Classes\Modules\Vouchers\Processors;


use App\Classes\Modules\Vouchers\Services\Voucherify\FetchesVoucherifyVoucher;
use App\Classes\Modules\Vouchers\Services\CreatesVoucher;
use App\Classes\Modules\Vouchers\Services\FetchesVoucher;
use App\Classes\Modules\Vouchers\DataTransferObjects\VoucherObject;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class CreateVoucherProcessor
{
    /** @var FetchesVoucherifyVoucher */
    private $fetchesVoucherifyVoucher;

    /** @var CreatesVoucher */
    private $createsVoucher;

    /** @var FetchesVoucher */
    private $fetchesVoucher;

    /**
     * CreateVoucherProcessor constructor.
     * @param FetchesVoucherifyVoucher $fetchesVoucherifyVoucher
     * @param CreatesVoucher $createsVoucher
     * @param FetchesVoucher $fetchesVoucher
     */
    public function __construct(FetchesVoucherifyVoucher $fetchesVoucherifyVoucher, CreatesVoucher $createsVoucher, FetchesVoucher $fetchesVoucher)
    {
        $this->fetchesVoucherifyVoucher = $fetchesVoucherifyVoucher;
        $this->createsVoucher = $createsVoucher;
        $this->fetchesVoucher = $fetchesVoucher;
    }


    /**
     * @param null|User $user
     * @param string $voucherCodeInput
     * @param null|int $campaignId
     * @return mixed
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(?User $user, string $voucherCodeInput, ?int $campaignId  = null) {
           //Remotely - Get Voucher Voucherify
           $voucherifyVoucherFetched = $this->fetchesVoucherifyVoucher->execute($user, $voucherCodeInput);
           $voucherName = null;
           $voucherDescription = null;

           //Voucher stored locally need a name, by default use campaign name, else look into campaign metadata for displayname
           if(isset($voucherifyVoucherFetched->metadata) && isset($voucherifyVoucherFetched->metadata->displayname)){
                $voucherName = $voucherifyVoucherFetched->metadata->displayname;
           }
           else{
                $voucherName = $voucherifyVoucherFetched->campaign;
           }

           if(isset($voucherifyVoucherFetched->metadata) && isset($voucherifyVoucherFetched->metadata->display_description)){
            $voucherDescription = $voucherifyVoucherFetched->metadata->display_description;
       }

           $voucherType = $voucherifyVoucherFetched->discount->type;
           $voucherValue = isset($voucherifyVoucherFetched->discount->amount_off) ? $voucherifyVoucherFetched->discount->amount_off : $voucherifyVoucherFetched->discount->percent_off;
           $voucherCode = $voucherifyVoucherFetched->code;
           $voucherStartDate = $voucherifyVoucherFetched->start_date;
           $voucherEndDate = $voucherifyVoucherFetched->expiration_date;

           if(isset($voucherifyVoucherFetched->discount->amount_off_formula)){
                $voucherType = 'PERCENT';
           }

           //Locally - Create and Fetch Voucher
           $voucherObject = new VoucherObject(
            $voucherCode,
            $voucherName ?? "Voucher ".Carbon::now()->format('Ymd'),
            $voucherDescription,
            $voucherType,
            $voucherValue,
            $campaignId,
            $voucherStartDate,
            $voucherEndDate);
           $voucher = $this->createsVoucher->execute($voucherObject);

           if(!$voucher) $voucher = $this->fetchesVoucher->execute(['code' => $voucherCodeInput]);
        return $voucher;
    }
}
