<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;

use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackageObject;
use App\Classes\Modules\PackingLists\Processors\CreatePackingListProcessor;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\PackingLists\Services\Packages\FetchesPackage;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\CreatesPackingList;
use App\Classes\Modules\PackingLists\Services\CreatesPackingListPackage;
use App\Classes\Modules\PackingLists\Standards\Rules\CanCreatePackingList;
use App\Classes\Modules\PackingLists\Standards\Rules\CanCreatePackingListPackage;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackingListType;

use App\Http\Resources\PackingListResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreatePackingListLogic 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 FetchesOrder */
    private $fetchesOrder;

    /**
     * CreatePackingListLogic constructor.
     * @param CreatePackingListProcessor $createPackingListProcessor
     * @param FetchesOrder $fetchesOrder
     */
    public function __construct(CreatePackingListProcessor $createPackingListProcessor, FetchesOrder $fetchesOrder)
    {
        $this->createPackingListProcessor = $createPackingListProcessor;
        $this->fetchesOrder = $fetchesOrder;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function logic(Request $request) : JsonResponse
    {

        $order = $this->fetchesOrder->execute(['reference' => $request->input('reference_number')]);

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

        $packingList = $this->createPackingListProcessor->execute($packingListObject, $order);

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

    }

}
