<?php

namespace App\Classes\Modules\Orders\Services\Steps;


use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\DataTransferObjects\StepObject;
use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Models\OrderStep;
use App\Http\Resources\OrderStep as OrderStepResources;
use Illuminate\Http\JsonResponse;

class UpdatesStepCompleteDate
{
    /** @var  OrderStep */
    private $repository;

    /**
     * UpdatesOrderReceiveNoteDate constructor.
     * @param OrderStep $repository
     */
    public function __construct(OrderStep $repository)
    {
        $this->repository = $repository;
    }

    /**
     * @param string $orderId
     * @param StepObject $step
     * @return JsonResponse
     * @throws InternalServerErrorException
     */
    public function execute(string $orderId, StepObject $step){

        try{
            $repository = $this->repository->where('order_id',$orderId)->where('reference_id',$step->getReferenceId())->first();
            $repository->complete_date = $step->getCompleteDate();
            $repository->save();

            return new JsonResponse([], HttpStatus::OK);

        } catch (\Exception $exception){
            throw new InternalServerErrorException("Failed to update step complete date because {$exception->getMessage()}");
        }


    }

}