<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Processors\CreatePackingListProcessor;
use App\Classes\Modules\PackingLists\Processors\CreatePackageProcessor;
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\PackingLists\Services\FetchesPackingList;
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 CreateWarehouseTransportLogic extends AbstractControllerLogic
{

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

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

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

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

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

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

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

    public function __construct(
        FetchesPackingList $fetchesPackingList,
        CreatePackingListProcessor $createPackingListProcessor,
        CreatesTransport $createsTransport,
        CreatesSchedule $createsSchedule,
        CreatePackageProcessor $createPackageProcessor,
        GeneratesPackingListReferenceNumber $generatesPackingListReferenceNumber
    ) {
        $this->fetchesPackingList = $fetchesPackingList;
        $this->createPackingListProcessor = $createPackingListProcessor;
        $this->createsTransport = $createsTransport;
        $this->createsSchedule = $createsSchedule;
        $this->createPackageProcessor = $createPackageProcessor;
        $this->generatesPackingListReferenceNumber = $generatesPackingListReferenceNumber;
    }

    /**
     * @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
    {
        $packing_list = $this->fetchesPackingList->execute([
            'id' => $request->route('id'),
        ]);

        $count_warehouse_packing_list = PackingList::where('reference', $packing_list->reference)->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->count();

        if ($count_warehouse_packing_list) {
            throw new MalformedRequestException('Warehouse Receive List Exists');
        }

        $order = $packing_list->owner;

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

        $packingListObject = new PackingListObject(
            $packing_list->reference,
            $order->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee->id,
            PackingListType::WAREHOUSE_RECEIVE_LIST,
            ApprovalStatus::PENDING_SUBMISSION
        );
        $packingList = $this->createPackingListProcessor->execute($packingListObject, $order);

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

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

        $packages = $packing_list->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));
    }
}
