<?php

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

/**
 * Cluster Nodes (Phase 3 hazırlık).
 *
 * Master plan Phase 3 — multi-server topology. Bir control plane + N node agent.
 * Phase 1'de UI + DB hazır, gerçek agent komünikasyonu Phase 3'te.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('cluster_nodes', function (Blueprint $table) {
            $table->id();

            $table->string('name', 100);
            $table->string('hostname', 191)->unique();
            $table->string('ip_address', 45);
            $table->string('public_ipv6', 60)->nullable();

            $table->enum('role', ['control_plane', 'web', 'database', 'dns', 'mail', 'cache', 'storage'])
                ->default('web');
            $table->enum('environment', ['production', 'staging', 'development'])->default('production');

            // Resource specs
            $table->unsignedInteger('cpu_cores')->nullable();
            $table->unsignedInteger('ram_mb')->nullable();
            $table->unsignedInteger('disk_gb')->nullable();
            $table->string('region', 50)->nullable();
            $table->string('provider', 50)->nullable()
                ->comment('Hetzner, DigitalOcean, AWS, vd.');

            // Connection
            $table->string('agent_url', 191)->nullable()
                ->comment('Node agent\'ın gRPC veya HTTP endpoint\'i');
            $table->text('agent_token')->nullable()
                ->comment('Encrypted shared secret');
            $table->string('ssh_user', 100)->default('root');
            $table->unsignedSmallInteger('ssh_port')->default(22);

            // Status
            $table->enum('status', ['provisioning', 'active', 'maintenance', 'down', 'decommissioned'])
                ->default('provisioning');
            $table->timestamp('last_heartbeat_at')->nullable();
            $table->unsignedInteger('current_account_count')->default(0);
            $table->unsignedInteger('max_account_count')->nullable();

            $table->json('software_versions')->nullable()
                ->comment('Apache, PHP, MariaDB, vd. versiyonları');
            $table->json('tags')->nullable();

            $table->boolean('accept_new_accounts')->default(true);
            $table->text('notes')->nullable();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['status', 'role']);
            $table->index('region');
        });
    }

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