<?php

namespace App\Classes\Modules\Orders\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\Orders\Processors\CreateOrderProcessor;
use App\Classes\Modules\Orders\Services\GeneratesOrderNumber;
use App\Classes\ValueObjects\Constants\WarehouseReferences;
use App\Http\Resources\OrderResource;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class CreateOrderLogic extends AbstractControllerLogic
{

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

    /** @var FetchesCompany */
    private $fetchesCompany;

    /** @var fetchesAddress */
    private $fetchesAddress;

    /** @var CreateOrderProcessor  */
    private $createOrderProcessor;

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

    /** @var GeneratesOrderNumber */
    private $generatesOrderNumber;

    /**
     * CreateOrderLogic constructor.
     * @param FetchesCompany $fetchesCompany
     * @param FetchesAddress $fetchesAddress
     * @param CreateOrderProcessor $createOrderProcessor
     * @param FetchesCompanyModule $fetchesCompanyModule
     * @param GeneratesOrderNumber $generatesOrderNumber
     */
    public function __construct(FetchesCompany $fetchesCompany, FetchesAddress $fetchesAddress, CreateOrderProcessor $createOrderProcessor, FetchesCompanyModule $fetchesCompanyModule, GeneratesOrderNumber $generatesOrderNumber)
    {
        $this->fetchesCompany = $fetchesCompany;
        $this->fetchesAddress = $fetchesAddress;
        $this->createOrderProcessor = $createOrderProcessor;
        $this->fetchesCompanyModule = $fetchesCompanyModule;
        $this->generatesOrderNumber = $generatesOrderNumber;
    }


    public function logic(Request $request) : JsonResponse {

        
        $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]);
        
        $address = $this->fetchesAddress->execute(['id' => $request->input('address_id')]);
        
        $originWarehouse = $this->fetchesCompanyModule->execute(['id' => $request->input('warehouse_id')]);
        
        if($originWarehouse->reference === WarehouseReferences::VT_GUANG_ZHOU){
            $marking = $company->companyModules()->importers()->first()->inviters()->withPivot('invitee_reference')->first()->pivot->invitee_reference;
            if(in_array($marking, ['2192KAA', '2353GFE', '6866DTR', '1905JYW', '1291NSC', '8288MIB'])){
                $originWarehouse = $this->fetchesCompanyModule->execute(['reference' => WarehouseReferences::YD_GUANG_ZHOU]);
            }
        }

        $order = $this->createOrderProcessor->execute($company, $originWarehouse, $address, $this->generatesOrderNumber->execute());

        return $this->resourceResponse(new OrderResource($order));
    }
}
