<?php

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


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Containers\DeletesContainer;
use App\Classes\Modules\PackingLists\Services\Containers\FetchesContainer;
use App\Classes\Modules\PackingLists\Standards\Rules\Containers\CanDeleteContainer;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

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


    /** @var CanDeleteContainer */
    private $canDeleteContainer;

    /** @var DeletesContainer */
    private $deletesContainer;

    /** @var FetchesContainer */
        private $fetchesContainer;

    /**
     * DeleteContainerControllersLogic constructor.
     * @param CanDeleteContainer $canDeleteContainer
     * @param DeletesContainer $deletesContainer
     * @param FetchesContainer $fetchesContainer
     */
    public function __construct(CanDeleteContainer $canDeleteContainer, DeletesContainer $deletesContainer, FetchesContainer $fetchesContainer)
    {
        $this->canDeleteContainer = $canDeleteContainer;
        $this->deletesContainer = $deletesContainer;
        $this->fetchesContainer = $fetchesContainer;
    }


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

            $this->canDeleteContainer->passes();

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

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

            return $this->response([]);

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

    }

}
