<?php

namespace App\Classes\Modules\Vouchers\ControllersLogic;


use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Vouchers\Processors\CreateVoucherProcessor;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;


class CreateVoucherLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create Voucher',
            'message' => 'You have successfully created a voucher'
        ];
    }

    /** @var CreateVoucherProcessor */
    private $createVoucherProcessor;

    /**
     * CreateVoucherLogic constructor.
     * @param CreateVoucherProcessor $createVoucherProcessor
     */
    public function __construct(CreateVoucherProcessor $createVoucherProcessor)
    {
        $this->createVoucherProcessor = $createVoucherProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $user = User::where('id', $request->input('userId'))->first();
        $result = $this->createVoucherProcessor->execute($user, $request->input('voucherCode'), null);
        return $this->response(['data' => $result]);
    }
}
