<?php

namespace App\Classes\Modules\Remarks\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Remarks\Services\UpdatesRemark;
use App\Classes\Modules\Remarks\Services\FetchesRemark;
use App\Classes\Modules\Remarks\Standards\Rules\CanUpdateRemark;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Http\Resources\RemarkResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateRemarkLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdateRemark */
    private $canUpdateRemark;

    /** @var UpdatesRemark */
    private $updatesRemark;

    /** @var FetchesRemark */
    private $fetchesRemark;

    /**
     * UpdateRemarkLogic constructor.
     * @param CanUpdateRemark $canUpdateRemark
     * @param UpdatesRemark $updatesRemark
     * @param FetchesRemark $fetchesRemark
     */
    public function __construct(CanUpdateRemark $canUpdateRemark, UpdatesRemark $updatesRemark, FetchesRemark $fetchesRemark)
    {
        $this->canUpdateRemark = $canUpdateRemark;
        $this->updatesRemark = $updatesRemark;
        $this->fetchesRemark = $fetchesRemark;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        $object = new RemarkObject($request->input('content'), auth()->user()->id);

        $this->canUpdateRemark->passes($object);

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

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

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

}
