<?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\Transports\Services\CreatesTransport;
use App\Classes\Modules\Schedules\Services\CreatesSchedule;
use App\Classes\Modules\PackingLists\DataTransferObjects\ContainerObject;
use App\Classes\Modules\Transports\DataTransferObjects\TransportObject;
use App\Classes\Modules\Schedules\DataTransferObjects\ScheduleObject;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Classes\ValueObjects\Constants\TransportType;
use App\Classes\ValueObjects\Constants\ApprovalStatus;

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

class CreateLoadContainerLogic 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;

    /** @var CreatesTransport */
    private $createsTransport;

    /** @var CreatesSchedule */
    private $createsSchedule;

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

    /**
     * @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'),
            ApprovalStatus::APPROVED
        );

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

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

        $transportObject = new TransportObject(
            TransportType::LAND, null, 
            $request->input('tracking'), 
            Carbon::parse($request->input('etd')), 
            Carbon::parse($request->input('eta')), 
            ApprovalStatus::APPROVED
        );
        $transport = $this->createsTransport->execute($transportObject, $container);

        $scheduleObject = new ScheduleObject(
            Carbon::parse($request->input('etd')), 
            Carbon::parse($request->input('eta')),
            ApprovalStatus::APPROVED
         );
        $schedule = $this->createsSchedule->execute($transport, $scheduleObject);

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

}