<?php

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

/**
 * DNS Zones + Records — PowerDNS gmysql schema uyumlu
 *
 * Phase 1 Epic 8
 *
 * Production'da PowerDNS bu tablolardan AUTH zone'ları okur.
 * Lokal dev'de FakeSysapi kullanır, gerçek DNS sorgusu yapılmaz.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('dns_zones', function (Blueprint $table) {
            $table->id();
            $table->foreignId('account_id')->constrained('accounts')->restrictOnDelete();
            $table->string('name')->unique()->comment('FQDN — onox.com.tr');
            $table->string('master', 128)->nullable();
            $table->string('type', 8)->default('NATIVE')->comment('NATIVE/MASTER/SLAVE');
            $table->unsignedInteger('notified_serial')->nullable();
            $table->unsignedInteger('last_check')->nullable();
            $table->string('powerdns_account', 40)->nullable();

            $table->enum('status', ['active', 'pending', 'error'])->default('pending');
            $table->text('last_error')->nullable();

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

            $table->index('account_id');
        });

        Schema::create('dns_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('zone_id')->constrained('dns_zones')->cascadeOnDelete();
            $table->string('name')->comment('FQDN — sub.onox.com.tr veya onox.com.tr (apex)');
            $table->string('type', 10)->comment('A/AAAA/CNAME/MX/TXT/SRV/CAA/NS/SOA/PTR');
            $table->text('content');
            $table->unsignedInteger('ttl')->default(3600);
            $table->unsignedInteger('prio')->nullable()->comment('MX/SRV priority');
            $table->boolean('disabled')->default(false);
            $table->boolean('auth')->default(true);

            $table->timestamps();

            $table->index(['zone_id', 'name', 'type']);
            $table->index(['name', 'type']);
        });
    }

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