<?php

namespace App\Provisioning\Steps\Customer;

use App\Models\ProvisioningRun;
use App\Provisioning\StepResult;

class ValidateOrder extends CustomerStep
{
    public function key(): string
    {
        return 'validate_order';
    }

    public function maxDuration(): int
    {
        return 60;
    }

    public function execute(ProvisioningRun $run): StepResult
    {
        $order = $this->order($run);

        if (! in_array($order->status, ['paid', 'provisioning'], true)) {
            return StepResult::fail('order_not_paid');
        }
        // Fail closed. No contract means nothing frozen to build from, and
        // sizing the machine from today's catalogue instead is exactly the bug
        // the snapshot exists to prevent.
        if ($this->plan($run) === []) {
            return StepResult::fail('no_subscription');
        }
        if (blank($order->datacenter)) {
            return StepResult::fail('invalid_datacenter');
        }

        $order->update(['status' => 'provisioning']);

        return StepResult::advance();
    }
}
