<?php

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

/**
 * PRC-1143 — konsolide şema: `ip_addresses` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('ip_addresses', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('pool_id');
            $t->string('address', 15);
            $t->unsignedBigInteger('address_long');
            $t->enum('status', ['free','used','quarantined','reserved'])->default('free');
            $t->unsignedBigInteger('client_product_id')->nullable();
            $t->timestamp('assigned_at')->nullable();
            $t->timestamp('released_at')->nullable();
            $t->string('rdns_record', 255)->nullable();
            $t->enum('allocated_via', ['requires_ip','config_option','manual_admin'])->nullable();
            $t->text('notes')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('client_product_id', 'ip_addresses_client_product_id_index');
            $t->unique(['pool_id', 'address'], 'ip_addresses_pool_id_address_unique');
            $t->index(['pool_id', 'status', 'address_long'], 'ip_addresses_pool_id_status_address_long_index');
            $t->index(['status', 'released_at'], 'ip_addresses_status_released_at_index');
            $t->foreign('client_product_id', 'ip_addresses_client_product_id_foreign')->references('id')->on('client_products')->onDelete('set null');
            $t->foreign('pool_id', 'ip_addresses_pool_id_foreign')->references('id')->on('ip_pools')->onDelete('cascade');
        });
    }

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