<?php
/**
 * Created by PhpStorm.
 * User: mhmj
 * Date: 17/06/2019
 * Time: 10:28 AM
 */

namespace App\Classes\Modules\Orders\Services;


use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\DataTransferObjects\ScheduleObject;
use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Models\Schedule;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateOrderSchedule
{
    /** @var  Schedule */
    private $repository;

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

    public function execute(String $scheduleId, ScheduleObject $schedule){
        try{

            $repository = $this->repository->findOrFail($scheduleId);
            $repository->ETD = $schedule->getETD();
            $repository->ETA = $schedule->getETA();
            $repository->issue_remark = $schedule->getIssue();
            $repository->save();
            return new JsonResponse([],HttpStatus::OK);

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


    }

}