<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * What Nextcloud was actually told, as opposed to what was sold.
 *
 * `quota_gb` is the allowance on the contract. For every instance built before
 * ApplyStorageQuota joined the pipelines it was also the whole story: the figure
 * sat in the row, nothing ever wrote it into the guest, and the customer kept
 * the entire disk whatever they had paid for. There was no way to tell those
 * instances apart from the ones where the step had run, because nothing recorded
 * that it had.
 *
 * This column is that record. Null means "we have never enforced an allowance on
 * this machine, and we do not know what it has" — which is deliberately what
 * every existing row gets, including ones the step has already visited: making
 * sure costs one idempotent occ call, and assuming costs the customer their
 * quota. `clupilot:apply-quotas` reads it to know what is left to repair, and
 * skips a machine whose enforced figure already matches its allowance.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('instances', function (Blueprint $table) {
            $table->unsignedInteger('quota_applied_gb')->nullable()->after('quota_gb');
        });
    }

    public function down(): void
    {
        Schema::table('instances', function (Blueprint $table) {
            $table->dropColumn('quota_applied_gb');
        });
    }
};
