<?php

namespace App\Classes\Modules\Orders\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Processors\CreatePackingListProcessor;
use App\Classes\Modules\PackingLists\Processors\CreatePackageProcessor;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\Transports\Services\CreatesTransport;
use App\Classes\Modules\PackingLists\Services\GeneratesPackingListReferenceNumber;
use App\Classes\Modules\Schedules\Services\CreatesSchedule;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackageObject;
use App\Classes\Modules\Transports\DataTransferObjects\TransportObject;
use App\Classes\Modules\Schedules\DataTransferObjects\ScheduleObject;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\TransportType;

use App\Http\Resources\PackingListResource;
use App\Models\PackingList;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Carbon\Carbon;

class CreateShippingPackingListLogic extends AbstractControllerLogic
{


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

    /** @var CreatePackingListProcessor */
    private $createPackingListProcessor;

     /** @var GeneratesPackingListReferenceNumber */
    private $generatesPackingListReferenceNumber;

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

    /** @var CreatePackageProcessor */
    private $createPackageProcessor;

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

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

    /**
     * CreatePackingListLogic constructor.
     * @param CreatePackingListProcessor $createPackingListProcessor
     * @param FetchesOrder $fetchesOrder
     */
    public function __construct(
        CreatePackingListProcessor $createPackingListProcessor, 
        CreatesTransport $createsTransport, 
        CreatesSchedule $createsSchedule,
        CreatePackageProcessor $createPackageProcessor,
        GeneratesPackingListReferenceNumber $generatesPackingListReferenceNumber, 
        FetchesOrder $fetchesOrder
    )
    {
        $this->createPackingListProcessor = $createPackingListProcessor;
        $this->createsTransport = $createsTransport;
        $this->createsSchedule = $createsSchedule;
        $this->createPackageProcessor = $createPackageProcessor;
        $this->generatesPackingListReferenceNumber = $generatesPackingListReferenceNumber;
        $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
    {
        $order = $this->fetchesOrder->execute(['id' => $request->input('order_id')]);

        $reference_number = $this->generatesPackingListReferenceNumber->execute();

        $packingListObject = new PackingListObject(
            $reference_number, 
            $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee->id, 
            PackingListType::SHIPPING_PACKING_LIST, 
            ApprovalStatus::PENDING_SUBMISSION
        );

        /** @var PackingList $packingList */
        $packingList = $this->createPackingListProcessor->execute($packingListObject, $order);

        $packages = $request->input('packages');

        foreach ($packages as $key => $row) {
            $packageObject = new PackageObject(
                $row['type'], 
                $row['description'], 
                $row['width'], 
                $row['height'], 
                $row['length'], 
                $row['weight'], 
                $row['quantity'], 
                ApprovalStatus::APPROVED,
                $row['reference'], 
            );
            $this->createPackageProcessor->execute($packageObject, $packingList);
        }

        return $this->resourceResponse(new PackingListResource($packingList));
    }

}
