<?php

namespace App\Classes\Modules\ControllersLogic\Orders\Attachments;

use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Orders\DataTransferObjects\ScheduleObject;
use App\Classes\Modules\Orders\DataTransferObjects\ShippingArrangementObject;
use App\Classes\Modules\Orders\DataTransferObjects\TrackingNumberObject;
use App\Classes\Modules\Orders\Services\Attachments\UpdatesSchedule;
use App\Classes\Modules\Orders\Services\Attachments\UpdatesShippingArrangement;
use App\Classes\Modules\Orders\Services\Attachments\UpdatesTrackingNumber;
use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Models\ShippingArrangement;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateShippingArrangementLogic
{

    /** @var UpdatesShippingArrangement */
    private $updatesShippingArrangement;

    /** @var UpdatesSchedule */
    private $updatesSchedule;

    /**
     * UpdateShippingArrangementLogic constructor.
     * @param UpdatesShippingArrangement $updatesShippingArrangement
     * @param UpdatesSchedule $updatesSchedule
     */
    public function __construct(UpdatesShippingArrangement $updatesShippingArrangement, UpdatesSchedule $updatesSchedule)
    {
        $this->updatesShippingArrangement = $updatesShippingArrangement;
        $this->updatesSchedule = $updatesSchedule;
    }


    /**
     * @param String $orderId
     * @param Request $request
     * @return JsonResponse
     * @throws InternalServerErrorException
     */
    public function execute(String $orderId, Request $request){

        try {

            $shippingArrangementObject = new ShippingArrangementObject($request->input('container_no'), $request->input('seal_no'));
            $arrangementId = $this->updatesShippingArrangement->execute($orderId, $shippingArrangementObject);

            $scheduleObject = new ScheduleObject($request->input('schedule')['ETA'], $request->input('schedule')['ETD'], 0, $request->input('schedule')['remark']);

            $this->updatesSchedule->execute($arrangementId, $scheduleObject);

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

        } catch (\Exception $exception){
            throw new InternalServerErrorException("Failed to update order's shipping arrangement because {$exception->getMessage()}");
        }



    }
}