<?php

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


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\PackingLists\Services\Containers\CreatesContainer;
use App\Classes\Modules\PackingLists\Standards\Rules\Containers\CanCreateContainer;
use App\Classes\Modules\PackingLists\DataTransferObjects\ContainerObject;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;

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

class CreateContainerLogic extends AbstractControllerLogic
{

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

    /** @var CanCreateContainer */
    private $canCreateContainer;

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

     /** @var FetchesCompanyModule */
    private $fetchesCompanyModule;

    /** @var CreatesPackingList */
    private $createsContainer;

    public function __construct(CanCreateContainer $canCreateContainer, FetchesPackingList $fetchesPackingList, FetchesCompanyModule $fetchesCompanyModule, CreatesContainer $createsContainer)
    {
        $this->canCreateContainer = $canCreateContainer;
        $this->fetchesPackingList = $fetchesPackingList;
        $this->fetchesCompanyModule = $fetchesCompanyModule;
        $this->createsContainer = $createsContainer;
    }

    /**
     * @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 ContainerObject(
            $request->input('container_reference'), 
            $request->input('container_number'), 
            $request->input('seal_reference'),
            $request->input('container_type'),
            $request->input('loading_date'),
            $request->input('status')
        );

        $this->canCreateContainer->passes($object);

        $query = $this->createsContainer->execute($object, $this->fetchesCompanyModule->execute(['id' => $request->input('company_module_id')]));

        return $this->resourceResponse(new ContainerResource($query));
    }

}
