<?php

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


use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Orders\DataTransferObjects\PackingListObject;
use App\Models\PackingList;

class UpdatesPackingList
{

    /** @var PackingList */
    private $repository;

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


    public function execute(int $orderId, PackingListObject $packingListObject, bool $confirmed){

        $packingList = $this->repository->updateOrCreate(['order_id' => $orderId],
            [
                'reference_no' => $packingListObject->getReferenceId(),
                'confirmed' => $confirmed
            ]);

        $packingList->packages()->delete();
        $packingList->packages()->createMany($packingListObject->getPackages());
    }

}