<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic\Containers;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Containers\CreatesContainerRemark;
use App\Classes\Modules\Remarks\Standards\Rules\CanCreateRemark;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\PackingLists\Services\Containers\FetchesContainer;

use App\Http\Resources\RemarkResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignContainerRemarkLogic 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 CreatesContainerRemark */
    private $createsContainerRemark;

    /** @var FetchesContainer */
    private $fetchesContainer;

    /**
     * CreateRemarkLogic constructor.
     * @param CanCreateRemark $canCreateRemark
     * @param CreatesContainerRemark $createsContainerRemark
     */
    public function __construct(CanCreateRemark $canCreateRemark, CreatesContainerRemark $createsContainerRemark, FetchesContainer $fetchesContainer)
    {
        $this->canCreateRemark = $canCreateRemark;
        $this->createsContainerRemark = $createsContainerRemark;
        $this->fetchesContainer = $fetchesContainer;
    }

    /**
     * @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->createsContainerRemark->execute($this->fetchesContainer->execute(['id' => $request->route('id')]), $object);
        return $this->resourceResponse( new RemarkResource($query));
    }
}
