<?php

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

// Per-WHMCS-server nameserver + IP "truth" table. WHMCS's GetServers API
// does not expose the nameserver1..4 fields admins enter in the WHMCS
// admin → Servers section, so we mirror them here. The NameserverDiagnostic
// service joins customer → product → serverid → this row to know which
// nameservers a given customer SHOULD be using and what IP their domain
// should resolve to.
return new class extends Migration {
    public function up(): void
    {
        Schema::create('whmcs_server_dns_configs', function (Blueprint $t) {
            $t->id();
            // WHMCS's internal server id (from GetServers.id). PK lookup
            // — one row per WHMCS server. Not a FK because we never store
            // WHMCS server rows locally; we just key by the remote id.
            $t->unsignedInteger('whmcs_server_id')->unique();
            $t->string('hostname', 191)->nullable();
            $t->string('ipaddress', 45)->nullable();
            // Nameservers admin enters in WHMCS server section. JSON array
            // of FQDNs in order — typically 2 or 4 entries. The customer's
            // domain MUST be delegated to these to receive hosting.
            $t->json('nameservers')->nullable();
            // Optional expected IPs for the nameservers themselves. If
            // populated, we also check that each NS resolves to the right
            // IP (catches the rare case where customer pointed at NS but
            // the NS records are stale / hijacked).
            $t->json('nameserver_ips')->nullable();
            // Free-text notes — e.g. "this server is on box-5, customers
            // sometimes confuse it with box-15". Surfaced to staff in the
            // diagnostic note.
            $t->text('notes')->nullable();
            // Soft enable — admin can disable a server's DNS check without
            // deleting the config (e.g. while migrating between IPs).
            $t->boolean('enabled')->default(true);
            $t->timestamps();
        });
    }

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