<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('name_servers', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('Nume descriptiv server');
            $table->string('hostname')->unique()->comment('Hostname NS (ex: ns1.example.com)');
            $table->string('ip_address')->comment('IP Address principal');
            $table->string('ipv6_address')->nullable()->comment('IPv6 Address (opțional)');
            
            // API Configuration
            $table->string('api_url')->nullable()->comment('URL API PowerDNS');
            $table->string('api_key')->nullable()->comment('API Key pentru autentificare');
            $table->string('api_type')->default('powerdns')->comment('Tip API: powerdns, bind, etc');
            $table->integer('api_port')->default(8081)->comment('Port API');
            $table->string('server_id')->default('localhost')->comment('Server ID pentru API');
            
            // Geolocation & Provider
            $table->string('country_code', 2)->nullable()->comment('Cod țară (RO, US, etc)');
            $table->string('city')->nullable()->comment('Oraș');
            $table->string('region')->nullable()->comment('Regiune/Continent (EU, NA, AS, etc)');
            $table->decimal('latitude', 10, 7)->nullable()->comment('Latitudine');
            $table->decimal('longitude', 10, 7)->nullable()->comment('Longitudine');
            $table->string('provider')->nullable()->comment('Provider hosting (AWS, DigitalOcean, etc)');
            $table->string('datacenter')->nullable()->comment('Datacenter location');
            
            // Status & Performance
            $table->boolean('is_active')->default(true)->comment('Server activ');
            $table->boolean('is_primary')->default(false)->comment('Server NS primar');
            $table->integer('priority')->default(10)->comment('Prioritate (mai mic = mai important)');
            $table->integer('weight')->default(100)->comment('Weight pentru load balancing');
            $table->timestamp('last_check_at')->nullable()->comment('Ultima verificare status');
            $table->string('status')->default('unknown')->comment('Status: online, offline, degraded, unknown');
            $table->integer('response_time_ms')->nullable()->comment('Timp răspuns în ms');
            
            // Limits & Quotas
            $table->integer('max_zones')->nullable()->comment('Număr maxim zone DNS');
            $table->integer('current_zones')->default(0)->comment('Număr curent zone DNS');
            
            // Notes & Description
            $table->text('description')->nullable()->comment('Descriere server');
            $table->text('notes')->nullable()->comment('Note administrative');
            
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('is_active');
            $table->index('is_primary');
            $table->index('status');
            $table->index('country_code');
            $table->index('region');
            $table->index('provider');
            $table->index(['is_active', 'priority']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('name_servers');
    }
};
