<?php

namespace App\Classes\Modules\Contacts\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Contacts\Services\DeletesContact;
use App\Classes\Modules\Contacts\Services\FetchesContact;
use App\Classes\Modules\Contacts\Standards\Rules\CanDeleteContact;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

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


    /** @var CanDeleteContact */
    private $canDeleteContact;

    /** @var DeletesContact */
    private $deletesContact;

    /** @var FetchesContact */
        private $fetchesContact;

    /**
     * DeleteContactControllerLogic constructor.
     * @param CanDeleteContact $canDeleteContact
     * @param DeletesContact $deletesContact
     * @param FetchesContact $fetchesContact
     */
    public function __construct(CanDeleteContact $canDeleteContact, DeletesContact $deletesContact, FetchesContact $fetchesContact)
    {
        $this->canDeleteContact = $canDeleteContact;
        $this->deletesContact = $deletesContact;
        $this->fetchesContact = $fetchesContact;
    }


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

            $this->canDeleteContact->passes();

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

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

            return $this->response([]);

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

    }

}