<?php

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

/**
 * Tracks whether a number actually has WhatsApp.
 *   has_whatsapp: null = unknown (not checked yet), 1 = yes, 0 = no.
 * Learned lazily from the first send outcome (or an explicit bridge check),
 * then used to skip wasted sends to non-WhatsApp numbers.
 */
return new class extends Migration {
    public function up(): void
    {
        if (!Schema::hasTable('wa_contacts') || Schema::hasColumn('wa_contacts', 'has_whatsapp')) {
            return;
        }

        Schema::table('wa_contacts', function (Blueprint $table) {
            $table->boolean('has_whatsapp')->nullable()->after('name')->index();
            $table->timestamp('wa_checked_at')->nullable()->after('has_whatsapp');
        });
    }

    public function down(): void
    {
        if (Schema::hasTable('wa_contacts') && Schema::hasColumn('wa_contacts', 'has_whatsapp')) {
            Schema::table('wa_contacts', function (Blueprint $table) {
                $table->dropColumn(['has_whatsapp', 'wa_checked_at']);
            });
        }
    }
};
