<?php

namespace App\Classes\Modules\Orders\Services;

use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\DataTransferObjects\AddressObject;
use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Models\Order;
use Illuminate\Http\JsonResponse;

class UpdatesOrderAddress
{
    /** @var  Order */
    private $repository;

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


    public function execute(string $orderId,string $addressId){
        try{

            $repository = $this->repository->findOrFail($orderId);
            $repository->address_id = $addressId;
            $repository->save();
            return new JsonResponse([],HttpStatus::OK);

        }catch (\Exception $exception){

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