<?php

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

/**
 * Stores the exact WhatsApp JID a contact addresses us from (e.g. a privacy
 * "LID" like 12345@lid). We key identity by phone number where possible, but
 * must reply to this original JID for the message to route. Persisted so it
 * survives bridge restarts.
 */
return new class extends Migration {
    public function up(): void
    {
        if (!Schema::hasTable('wa_contacts') || Schema::hasColumn('wa_contacts', 'address_jid')) {
            return;
        }

        Schema::table('wa_contacts', function (Blueprint $table) {
            $table->string('address_jid', 60)->nullable()->after('wa_id');
        });
    }

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