<?php

namespace App\Classes\Modules\Remarks\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\Remarks\Processors\CreateRemarkProcessor;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Classes\Exceptions\MalformedRequestException;
use App\Http\Resources\RemarkResource;

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

    /** @var CreateRemarkProcessor */
    private $createRemarkProcessor;

    /**
     * CreateRemarkLogic constructor.
     * @param CreateRemarkProcessor $createRemarkProcessor
     */
    public function __construct(CreateRemarkProcessor $createRemarkProcessor)
    {
        $this->createRemarkProcessor = $createRemarkProcessor;
    }

    /**
     * @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
    {
        $classs = '\\App\\Models\\' . Str::studly($request->input('model_type'));

        if (!class_exists($classs)) {
            throw new MalformedRequestException('Unable to process this entity');
        }
        
        $remarkOwner = $classs::find($request->route('id'));

        $remarkObject = new RemarkObject($request->input('content'), auth()->user()->id);

        $remmark = $this->createRemarkProcessor->execute($remarkOwner, $remarkObject);

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