<?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::table('dns_zone_statistics', function (Blueprint $table) {
            // Drop foreign key constraint
            $table->dropForeign(['nameserver_id']);
            
            // Make nameserver_id nullable
            $table->unsignedBigInteger('nameserver_id')->nullable()->change();
            
            // Re-add foreign key without ON DELETE CASCADE (pentru a permite NULL)
            $table->foreign('nameserver_id')
                  ->references('id')
                  ->on('name_servers')
                  ->nullOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('dns_zone_statistics', function (Blueprint $table) {
            // Drop foreign key
            $table->dropForeign(['nameserver_id']);
            
            // Make nameserver_id NOT NULL again
            $table->unsignedBigInteger('nameserver_id')->nullable(false)->change();
            
            // Re-add original foreign key with CASCADE
            $table->foreign('nameserver_id')
                  ->references('id')
                  ->on('name_servers')
                  ->onDelete('cascade');
        });
    }
};
