<?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\Standards\Rules\CanUpdatePackingList;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Http\Resources\PackingListResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdatePackingListLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdatePackingList */
    private $canUpdatePackingList;

    /** @var UpdatesPackingList */
    private $updatesPackingList;

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

    /**
     * UpdatePackingListLogic constructor.
     * @param CanUpdatePackingList $canUpdatePackingList
     * @param UpdatesPackingList $updatesPackingList
     * @param FetchesPackingList $fetchesPackingList
     */
    public function __construct(CanUpdatePackingList $canUpdatePackingList, UpdatesPackingList $updatesPackingList, FetchesPackingList $fetchesPackingList)
    {
        $this->canUpdatePackingList = $canUpdatePackingList;
        $this->updatesPackingList = $updatesPackingList;
        $this->fetchesPackingList = $fetchesPackingList;
    }

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

        $object = new PackingListObject($packingList->reference, $packingList->claimant_id, $packingList->type, $request->input('status'));

        $this->canUpdatePackingList->passes($object);

        $query = $this->updatesPackingList->execute($packingList, $object);

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

}
