<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\UpdatesPackingList;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Classes\Modules\PackingLists\Services\UpdatesPackingListStatus;
use App\Classes\Modules\PackingLists\Standards\Rules\CanUpdatePackingList;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Classes\Modules\Orders\Processors\UpdateDoFromVTPortalProcessor;
use App\Http\Resources\PackingListResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdatePackingListStatusLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated PackingList Status',
            'message' => 'You have successfully updated the PackingList status'
        ];
    }


    /** @var FetchesPackingList */
    private $fetchesPackingList;

    /** @var UpdatesPackingListStatus */
    private $updatesPackingListStatus;

    /** @var UpdateDoFromVTPortalProcessor */
    private $updateDoFromVTPortalProcessor;

    /**
     * UpdatePackingListStatusLogic constructor.
     * @param FetchesPackingList $fetchesPackingList
     * @param UpdatesPackingListStatus $updatesPackingListStatus
     */
    public function __construct(FetchesPackingList $fetchesPackingList, UpdatesPackingListStatus $updatesPackingListStatus, UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor)
    {
        $this->fetchesPackingList = $fetchesPackingList;
        $this->updatesPackingListStatus = $updatesPackingListStatus;
        $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $packingList = $this->fetchesPackingList->execute(['id' => $request->route('id')]);

        $packing_list = $this->updatesPackingListStatus->execute($packingList, $request->route('status'));
        $address = $packing_list->owner()->first()->addresses()->first();

        $vt_do_update = $this->updateDoFromVTPortalProcessor->execute($address);

        return $this->resourceResponse(new PackingListResource($packing_list));
    }

}
