<?php

declare(strict_types=1);

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

/**
 * DNS Templates + Template Records — Admin tanımlı yeniden kullanılabilir
 * DNS şablonları. WHM "DNS Zone Template" muadili.
 *
 * Yeni zone oluşturulurken:
 *   1. Explicit template_id verilirse → o template
 *   2. Account paketine atanmış template varsa → o template
 *   3. is_default=true olan global template
 *   4. Hiçbiri yoksa → DnsRecordTemplate (hardcoded fallback)
 *
 * Placeholder substitution apply zamanında:
 *   ${DOMAIN}      → zone'un FQDN'i
 *   ${SERVER_IP}   → server primary IP
 *   ${MAIL_HOST}   → mail.${DOMAIN} (override edilebilir)
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('dns_templates', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique()->comment('Görüntü adı — "WordPress Default", "Google Workspace"');
            $table->string('slug')->unique()->comment('lowercase + dash, URL safe');
            $table->text('description')->nullable();

            $table->boolean('is_default')->default(false)
                ->comment('Tam olarak bir satır true olmalı (global default)');
            $table->boolean('is_active')->default(true);

            $table->foreignId('applies_to_package_id')->nullable()
                ->constrained('packages')->nullOnDelete()
                ->comment('Sadece bu paketin zone\'larında kullanılır; null=global');

            // ${SERVER_IP}, ${MAIL_HOST} gibi runtime substitution değerleri
            // Örn: {"server_ip":"198.51.100.42","mail_host":"mail.example.com"}
            $table->json('placeholders')->nullable();

            $table->foreignId('created_by')->nullable()
                ->constrained('users')->nullOnDelete();

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

            $table->index('applies_to_package_id');
            $table->index(['is_active', 'is_default']);
        });

        Schema::create('dns_template_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('dns_template_id')
                ->constrained('dns_templates')
                ->cascadeOnDelete();

            // name: '@' = apex, 'mail.${DOMAIN}', 'www', '_dmarc.${DOMAIN}'
            $table->string('name')->comment('Placeholder destekli: @, ${DOMAIN}, mail.${DOMAIN}');

            $table->enum('type', ['A', 'AAAA', 'CNAME', 'MX', 'TXT', 'SRV', 'CAA', 'NS']);

            // content: '${SERVER_IP}', '${MAIL_HOST}', 'v=spf1 +a +mx +ip4:${SERVER_IP} ~all'
            $table->text('content');

            $table->unsignedInteger('ttl')->default(3600);
            $table->unsignedInteger('prio')->nullable()
                ->comment('MX/SRV priority');
            $table->unsignedInteger('sort_order')->default(0)
                ->comment('Apply sırası — SOA sonra A sonra MX vb.');

            $table->timestamps();

            $table->index(['dns_template_id', 'sort_order']);
        });
    }

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