<?php

use App\Models\Instance;
use App\Models\Order;
use App\Models\ProvisioningRun;
use App\Provisioning\Steps\Customer\ApplyStorageQuota;
use App\Services\Billing\PlanCatalogue;
use Tests\Support\Steps\ProbeCustomerStep;

it('resolves order, instance and plan from the run', function () {
    $order = Order::factory()->withSubscription()->create(['plan' => 'team']);
    $instance = Instance::factory()->create();
    $run = ProvisioningRun::factory()->create([
        'subject_type' => Order::class,
        'subject_id' => $order->id,
        'pipeline' => 'customer',
        'context' => ['instance_id' => $instance->id],
    ]);

    $step = new ProbeCustomerStep;

    expect($step->orderOf($run)->is($order))->toBeTrue()
        ->and($step->instanceOf($run)->is($instance))->toBeTrue()
        ->and($step->planOf($run)['quota_gb'])->toBe(85)
        ->and($step->label())->toBe('provisioning.step.probe');
});

it('builds every customer machine with the storage allowance that was sold', function () {
    // A count alone only says the list is as long as it was. What matters is
    // that the quota step is IN it: the allowance was applied nowhere for the
    // whole life of the pipeline — quota_gb reached the instance row and
    // stopped — so every package handed out the whole disk.
    expect(config('provisioning.pipelines.customer'))->toHaveCount(17)
        ->toContain(ApplyStorageQuota::class);
});

it('takes the blueprint from the catalogue, which no longer lives in config', function () {
    expect(config('provisioning.plans'))->toBeNull()
        ->and(app(PlanCatalogue::class)->currentVersion('start')->template_vmid)->toBe(9000);
});
