<?php

namespace App\Classes\Modules\Orders\ControllersLogic;

use App\Classes\Exceptions\RequestValidationException;
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(!in_array($company->companyModules()->importers()->first()->id, WarehouseReferences::YD_EXEMPT_LIST)){
            $originWarehouse = $this->fetchesCompanyModule->execute(['reference' =>  $originWarehouse->reference === WarehouseReferences::VT_GUANG_ZHOU ? WarehouseReferences::YD_GUANG_ZHOU : WarehouseReferences::YD_YIWU]);
        }

        if($originWarehouse->reference === WarehouseReferences::YD_YIWU && in_array($address->state_id, [5, 13, 14])) {
            throw new RequestValidationException('Our Yiwu warehouse is unable to ship goods to Sabah & Sarawak at the moment. you can select our Guangzhou warehouse as an alternative.');
        }

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

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