<?php

namespace App\Classes\Modules\Orders\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Orders\Services\CreatesOrderRemark;
use App\Classes\Modules\Remarks\Standards\Rules\CanCreateRemark;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Http\Resources\RemarkResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignOrderRemarkLogic extends AbstractControllerLogic
{

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

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

    /** @var CreatesOrderRemark */
    private $createsOrderRemark;

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

    /**
     * CreateRemarkLogic constructor.
     * @param CanCreateRemark $canCreateRemark
     * @param CreatesOrderRemark $createsOrderRemark
     */
    public function __construct(CanCreateRemark $canCreateRemark, CreatesOrderRemark $createsOrderRemark, FetchesOrder $fetchesOrder)
    {
        $this->canCreateRemark = $canCreateRemark;
        $this->createsOrderRemark = $createsOrderRemark;
        $this->fetchesOrder = $fetchesOrder;
    }

    /**
     * @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->route('id')
        );

        $this->canCreateRemark->passes($object);
        $query = $this->createsOrderRemark->execute($this->fetchesOrder->execute(['id' => $request->route('id')]), $object);
        return $this->resourceResponse( new RemarkResource($query));
    }
}
