<?php

namespace App\Classes\Modules\Orders\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\Remarks\Processors\CreateRemarkProcessor;
use App\Http\Resources\OrderResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

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

    /** @var FetchesOrder */
    private $fetchesOrder;

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

    /**
     * CreateOrderRemarkLogic constructor.
     * @param FetchesOrder $fetchesOrder
     * @param CreateRemarkProcessor $createRemarkProcessor
     */
    public function __construct(FetchesOrder $fetchesOrder, CreateRemarkProcessor $createRemarkProcessor)
    {
        $this->fetchesOrder = $fetchesOrder;
        $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
    {
        $order = $this->fetchesOrder->execute(['id' => $request->route('id')]);

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

        $this->createRemarkProcessor->execute($order, $object);

        return $this->resourceResponse(new OrderResource($order));

    }
}
