<?php

namespace App\Classes\Modules\FreightForwarder\Services;


use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\FreightForwarder\DataTransferObjects\ServiceObject;
use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Models\ServiceFees;
use Illuminate\Http\JsonResponse;

class UpdatesServiceFees
{
    /** @var  ServiceFees */
    private $repository;

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


    public function execute(ServiceObject $serviceObject, ?int $id = null){
        try{
            $service =  $id ? $this->repository->findOrFail($id) : new ServiceFees;

            $service->forwarder_id = $serviceObject->getForwarderId();
            $service->name = $serviceObject->getName();
            $service->type = $serviceObject->getType();
            $service->description = $serviceObject->getDescription();
            $service->cost = $serviceObject->getCost();

            $service->save();
             return $service;

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

        }catch(\Exception $exception){

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