<?php

namespace App\Classes\Modules\Contacts\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Contacts\Services\UpdatesContact;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Classes\Modules\Contacts\Services\FetchesContact;
use App\Http\Resources\ContactResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateContactLogic extends AbstractControllerLogic
{

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

    /** @var UpdatesContact */
    private $updatesContact;

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

    /**
     * UpdateContactLogic constructor.
     * @param UpdatesContact $updatesContact
     * @param FetchesContact $fetchesContact
     */
    public function __construct(UpdatesContact $updatesContact, FetchesContact $fetchesContact)
    {
        $this->updatesContact = $updatesContact;
        $this->fetchesContact = $fetchesContact;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $object = new ContactObject(
            $request->input('reference'),
            $request->input('phone'),
            $request->input('email'),
            $request->input('wechat_id')
        );

        // $this->canUpdateContact->passes($object);

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

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

        return $this->resourceResponse(new ContactResource($query));
    }

}