<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;

use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\PackingLists\Processors\CreatePackingListProcessor;
use App\Classes\Modules\PackingLists\Services\GeneratesPackingListReferenceNumber;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackingListType;

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

class CreateDeliverPackingListLogic extends AbstractControllerLogic
{


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

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

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

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

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

    /**
     * CreateDeliverPackingListLogic constructor.
     * @param CreatePackingListProcessor $createPackingListProcessor
     * @param FetchesCompanyModule $fetchesCompanyModule
     */
    public function __construct(CreatePackingListProcessor $createPackingListProcessor, FetchesCompanyModule $fetchesCompanyModule, GeneratesPackingListReferenceNumber $generatesPackingListReferenceNumber, FetchesPackingList $fetchesPackingList)
    {
        $this->createPackingListProcessor = $createPackingListProcessor;
        $this->fetchesCompanyModule = $fetchesCompanyModule;
        $this->generatesPackingListReferenceNumber = $generatesPackingListReferenceNumber;
        $this->fetchesPackingList = $fetchesPackingList;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function logic(Request $request) : JsonResponse
    {
        $company_module = $this->fetchesCompanyModule->execute(['id' => $request->input('company_module_id')]);
        $packing_list_id = $request->input('packing_list_id');

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

        $packingListObject = new PackingListObject(
            $reference_number, 
            null, 
            PackingListType::DELIVERY_PACKING_LIST, 
            ApprovalStatus::PENDING_SUBMISSION
        );

        $deliverPackingList = $this->createPackingListProcessor->execute($packingListObject, $company_module);

        foreach ($packing_list_id as $key => $row) {
            $this->fetchesPackingList->execute(['id' => $row]);
        }

        $deliverPackingList->packing_list_owner()->sync($packing_list_id);

        return $this->resourceResponse(new DeliverPackingListResource($deliverPackingList));
    }

}
