<?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('payments') && ! Schema::hasColumn('payments', 'activity_id')) {
            Schema::table('payments', function (Blueprint $table): void {
                $table->foreignId('activity_id')->nullable()->constrained('activities')->nullOnDelete()->after('created_by');
                $table->index('activity_id');
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('payments') && Schema::hasColumn('payments', 'activity_id')) {
            Schema::table('payments', function (Blueprint $table): void {
                $table->dropConstrainedForeignId('activity_id');
            });
        }
    }
};
