<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic\Packages;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Packages\DeletesPackage;
use App\Classes\Modules\PackingLists\Services\Packages\FetchesPackage;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\CanDeletePackage;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

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


    /** @var CanDeletePackage */
    private $canDeletePackage;

    /** @var DeletesPackage */
    private $deletesPackage;

    /** @var FetchesPackage */
        private $fetchesPackage;

    /**
     * DeletePackageControllersLogic constructor.
     * @param CanDeletePackage $canDeletePackage
     * @param DeletesPackage $deletesPackage
     * @param FetchesPackage $fetchesPackage
     */
    public function __construct(CanDeletePackage $canDeletePackage, DeletesPackage $deletesPackage, FetchesPackage $fetchesPackage)
    {
        $this->canDeletePackage = $canDeletePackage;
        $this->deletesPackage = $deletesPackage;
        $this->fetchesPackage = $fetchesPackage;
    }


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

            $this->canDeletePackage->passes();

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

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

            return $this->response([]);

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

    }

}
