<?php

namespace App\Classes\Modules\PackageItems\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Packages\Items\UpdatesPackageItem;
use App\Classes\Modules\PackingLists\Services\Packages\Items\FetchesItem;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\Items\CanUpdatePackageItem;
use App\Classes\Modules\PackingLists\DataTransferObjects\ItemObject;
use App\Http\Resources\ItemResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdatePackageItemLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdatePackageItem */
    private $canUpdatePackageItem;

    /** @var UpdatesPackageItem */
    private $updatesPackageItem;

    /** @var FetchesItem */
    private $fetchesPackageItem;

    /**
     * UpdatePackageItemLogic constructor.
     * @param CanUpdatePackageItem $canUpdatePackageItem
     * @param UpdatesPackageItem $updatesPackageItem
     * @param FetchesItem $fetchesPackageItem
     */
    public function __construct(CanUpdatePackageItem $canUpdatePackageItem, UpdatesPackageItem $updatesPackageItem, FetchesItem $fetchesPackageItem)
    {
        $this->canUpdatePackageItem = $canUpdatePackageItem;
        $this->updatesPackageItem = $updatesPackageItem;
        $this->fetchesPackageItem = $fetchesPackageItem;
    }


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

        $object = new ItemObject(
            $packageItem->package_id,
            $request->input('name'),
            $packageItem->reference,
            $request->input('description'),
            $request->input('quantity'),
            $request->input('uom'),
            $request->input('price'),
            $request->input('total'),
            $request->input('status'));

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

        $query = $this->updatesPackageItem->execute($packageItem, $object);

        return $this->resourceResponse(new ItemResource($query));

    }

}
