<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('instances', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('customer_id')->constrained('customers')->cascadeOnDelete();
            $table->foreignId('order_id')->constrained('orders')->cascadeOnDelete();
            $table->foreignId('host_id')->nullable()->constrained('hosts')->nullOnDelete();
            $table->unsignedInteger('vmid')->nullable();
            $table->string('plan');
            $table->unsignedBigInteger('quota_gb');
            $table->unsignedBigInteger('disk_gb');
            $table->unsignedInteger('ram_mb');
            $table->unsignedInteger('cores');
            $table->string('subdomain')->unique();
            $table->string('custom_domain')->nullable();
            $table->text('nc_admin_ref')->nullable();        // encrypted username/ref — never password
            $table->boolean('route_written')->default(false);
            $table->boolean('cert_ok')->default(false);
            $table->string('status')->default('reserving');  // reserving|provisioning|active|suspended|failed
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('instances');
    }
};
