<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\CreatesPackingListRemark;
use App\Classes\Modules\Remarks\Standards\Rules\CanCreateRemark;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Http\Resources\RemarkResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignPackingListRemarkLogic extends AbstractControllerLogic
{

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

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

    /** @var CreatesPackingListRemark */
    private $createsPackingListRemark;

    /** @var FetchesPackingList */
    private $fetchesPackingList;

    /**
     * CreateRemarkLogic constructor.
     * @param CanCreateRemark $canCreateRemark
     * @param CreatesPackingListRemark $createsPackingListRemark
     */
    public function __construct(CanCreateRemark $canCreateRemark, CreatesPackingListRemark $createsPackingListRemark, FetchesPackingList $fetchesPackingList)
    {
        $this->canCreateRemark = $canCreateRemark;
        $this->createsPackingListRemark = $createsPackingListRemark;
        $this->fetchesPackingList = $fetchesPackingList;
    }

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