<?php

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

return new class extends Migration {
    public function up(): void
    {
        if (!Schema::hasTable('wa_messages') || Schema::hasColumn('wa_messages', 'conversation_id')) {
            return;
        }

        Schema::table('wa_messages', function (Blueprint $table) {
            $table->unsignedBigInteger('conversation_id')->nullable()->after('channel_id')->index();
        });
    }

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