<?php

namespace App\Classes\Modules\PackageItems\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Packages\Items\DeletesItem;
use App\Classes\Modules\PackingLists\Services\Packages\Items\FetchesItem;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\Items\CanDeleteItem;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DeletePackageItemLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Deleted PackageItem',
            'message' => 'You have successfully deleted a PackageItem'
        ];
    }


    /** @var CanDeleteItem */
    private $canDeletePackageItem;

    /** @var DeletesItem */
    private $deletesPackageItem;

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

    /**
     * DeletePackageItemControllersLogic constructor.
     * @param CanDeleteItem $canDeletePackageItem
     * @param DeletesItem $deletesPackageItem
     * @param FetchesItem $fetchesPackageItem
     */
    public function __construct(CanDeleteItem $canDeletePackageItem, DeletesItem $deletesPackageItem, FetchesItem $fetchesPackageItem)
    {
        $this->canDeletePackageItem = $canDeletePackageItem;
        $this->deletesPackageItem = $deletesPackageItem;
        $this->fetchesPackageItem = $fetchesPackageItem;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {

            $this->canDeletePackageItem->passes();

            $query = $this->fetchesPackageItem->execute(['id' => $request->route('id')]);

            $this->deletesPackageItem->execute($query);

            return $this->response([]);

        } catch (\Exception $exception){
            throw new ErrorException($exception->getMessage(), $exception->getCode());
        }

    }

}
