<?php

namespace App\Classes\Modules\Remarks\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Remarks\Services\CreatesRemark;
use App\Classes\Modules\Remarks\Standards\Rules\CanCreateRemark;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\Accounts\Services\FetchesUser;
use App\Http\Resources\RemarkResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateRemarkLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Remark',
            'message' => 'You have successfully created a new Remark'
        ];
    }

    /** @var CanCreateRemark */
    private $canCreateRemark;

    /** @var CreatesRemark */
    private $createsRemark;

    /** @var FetchesUser */
    private $fetchesUser;

    /**
     * CreateRemarkLogic constructor.
     * @param CanCreateRemark $canCreateRemark
     * @param CreatesRemark $createsRemark
     */
    public function __construct(CanCreateRemark $canCreateRemark, CreatesRemark $createsRemark, FetchesUser $fetchesUser)
    {
        $this->canCreateRemark = $canCreateRemark;
        $this->createsRemark = $createsRemark;
        $this->fetchesUser = $fetchesUser;
    }

    /**
     * @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'), $request->input('commenter_id'));

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

        $query = $this->createsRemark->execute($this->fetchesUser->execute(['id' => $request->input('commenter_id')]), $object);

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

    }

}
