<?php

namespace App\Classes\Modules\Orders\Processors;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Orders\Services\CreatesOrder;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\Orders\Standards\Rules\CanConfirmOrder;
use App\Classes\Modules\OrderSteps\Processors\GenerateOrderStepsProcessor;
use App\Classes\ValueObjects\Constants\OrderStatus;
use App\Classes\ValueObjects\Constants\OrderSteps;
use App\Classes\ValueObjects\Constants\OrderType;
use App\Classes\Modules\OrderSteps\DataTransferObjects\StepsObject;
use App\Classes\Modules\OrderSteps\Services\UpdatesOrderSteps;
use Illuminate\Http\Request;

class ConfirmOrderProcessor
{
    private $canConfirmOrder;

    private $generateOrderStepsProcessor;

    private $updatesOrderCurrentStep;

    public function __construct(CanConfirmOrder $canConfirmOrder, GenerateOrderStepsProcessor $generateOrderStepsProcessor, FetchesOrder $fetchesOrder, UpdatesOrderSteps $updatesOrderStep)
    {
        $this->canConfirmOrder = $canConfirmOrder;
        $this->generateOrderStepsProcessor = $generateOrderStepsProcessor;
        $this->fetchesOrder = $fetchesOrder;
        $this->updatesOrderStep = $updatesOrderStep;
    }

    public function execute(int $orderId, array $contract_obligation_list = [], array $orderRoles=[] ){

        $order_object = new OrderObject(0,'','',0,0, '',$orderId,0,0);

        $this->canConfirmOrder->passes($order_object);

        $order = $this->fetchesOrder->execute(['id'=>$order_object->getId()]);

        //Only Processing Step is created at this point
        $orderStep = $order->orderSteps[0];

        $orderStepObject = new StepsObject(
            0,
            $orderStep->reference,
            true,
            $orderStep->sequence,
            OrderStatus::COMPLETE,
            0,
            date('Y-m-d H:i:s'),
            $orderStep->id,
            $orderStep->hash_id
        );

        //Update PROCESSING to completed
        $updatedOrderStep = $this->updatesOrderStep->execute($orderStep, $orderStepObject);

        //Generate Order Steps base on Contract from Unity
        $this->generateOrderStepsProcessor->execute($order, $contract_obligation_list, $orderRoles);

    }
}
