<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('fundings') && !Schema::hasColumn('fundings', 'chat_id')) {
            Schema::table('fundings', function (Blueprint $table) {
                $table->unsignedBigInteger('chat_id')->nullable()->after('wallet_address_id');
                $table->foreign('chat_id')->references('id')->on('p2p_chats')->onDelete('set null');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (Schema::hasTable('fundings') && Schema::hasColumn('fundings', 'chat_id')) {
            Schema::table('fundings', function (Blueprint $table) {
                $table->dropForeign(['chat_id']);
                $table->dropColumn('chat_id');
            });
        }
    }
}; 