<?php

namespace App\Classes\Modules\Orders\Processors;

use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Orders\DataTransferObjects\StepObject;
use App\Classes\Modules\Orders\Services\Steps\CompletesOrderStep;
use App\Classes\ValueObjects\Constants\HttpStatus;
use Illuminate\Http\JsonResponse;

abstract class AbstractStepProcessor
{

    abstract function validator(): bool;

    abstract function execute(): JsonResponse;

    /**
     * @param int $orderId
     * @param StepObject $step
     * @return JsonResponse
     * @throws InternalServerErrorException
     */
    public function handler(int $orderId, StepObject $step) {
        try {

            if(!$this->validator()){
                return new JsonResponse(['message' => 'Updating step validation has failed due to missing requirements'], HttpStatus::VALIDATION_FAILED);
            }

            (new CompletesOrderStep())->execute($orderId, $step);

            /** notification */


            return new JsonResponse(null, HttpStatus::REQUEST_ACCEPTED);

        } catch (\Exception $exception){

            throw new InternalServerErrorException("Failed to update order step because {$exception->getMessage()}");

        }
    }

}