<?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\TrackingNumberObject;
use App\Classes\Modules\Orders\Services\Attachments\UpdatesTrackingNumber;
use App\Classes\ValueObjects\Constants\HttpStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateTrackingNumberLogic
{

    /** @var UpdatesTrackingNumber */
    private $updatesTrackingNumber;

    /**
     * UpdateTrackingNumberLogic constructor.
     * @param UpdatesTrackingNumber $updatesTrackingNumber
     */
    public function __construct(UpdatesTrackingNumber $updatesTrackingNumber)
    {
        $this->updatesTrackingNumber = $updatesTrackingNumber;
    }

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

        try {
            $trackingNumberObject = new TrackingNumberObject($request->input('type'), $request->input('tracking_number'), $request->input('courier'));
            $this->updatesTrackingNumber->execute($orderId, $trackingNumberObject);

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

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



    }
}