<?php

namespace App\Classes\Modules\FreightForwarder\Services;

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

class UpdatesFreightForwarder
{
    /** @var Forwarder */
    private $repository;

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


    public function execute(String $forwarderId, FreightForwarderObject $forwarderObject){

        try{
            $repository = $this->repository->findOrFail($forwarderId);
            $repository->shipping_rate = $forwarderObject->getShippingRate();
            $repository->overdue_days = $forwarderObject->getOverdueDays();
            $repository->markup_percentage = $forwarderObject->getMarkupPercentage();
            $repository->minimum_cbm = $forwarderObject->getMinimumCbm();
            $repository->save();
            return new JsonResponse([],HttpStatus::OK);

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

}