<?php

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

// Wire the nameserver-diagnostic feature into existing tables. Tickets
// gain a timestamp + cached JSON so the scheduler can skip re-running on
// the same conversation, and admins can see the most-recent finding
// without re-fetching. Settings table gains the toggle.
return new class extends Migration {
    public function up(): void
    {
        Schema::table('tickets', function (Blueprint $t) {
            // When did we last post a nameserver-diagnostic admin note?
            // Anti-duplicate guard — same pattern as
            // last_admin_note_assist_at.
            $t->timestamp('last_ns_diagnostic_at')->nullable()->after('last_admin_note_assist_at');
            // Latest diagnostic result. Cached so the ticket detail UI
            // can show the verdict (mismatch / ok / no-config) without
            // re-running the lookup.
            $t->json('last_ns_diagnostic_result')->nullable()->after('last_ns_diagnostic_at');
        });

        Schema::table('ai_automation_settings', function (Blueprint $t) {
            $t->boolean('ns_diagnostic_enabled')->default(false)->after('admin_note_assist_enabled');
        });
    }

    public function down(): void
    {
        Schema::table('tickets', function (Blueprint $t) {
            $t->dropColumn(['last_ns_diagnostic_at', 'last_ns_diagnostic_result']);
        });
        Schema::table('ai_automation_settings', function (Blueprint $t) {
            $t->dropColumn('ns_diagnostic_enabled');
        });
    }
};
