<?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()
    {
        Schema::table('transfers', function (Blueprint $table) {
            // Add sender_id if it doesn't exist
            if (!Schema::hasColumn('transfers', 'sender_id')) {
                $table->foreignId('sender_id')->nullable()->constrained('users')->onDelete('cascade');
            }
            
            // Add recipient_id if it doesn't exist
            if (!Schema::hasColumn('transfers', 'recipient_id')) {
                $table->foreignId('recipient_id')->nullable()->constrained('users')->onDelete('cascade');
            }
            
            // Add amount column if it doesn't exist
            if (!Schema::hasColumn('transfers', 'amount')) {
                $table->decimal('amount', 12, 2)->nullable();
            }
            
            // Add destination column for wallet type
            if (!Schema::hasColumn('transfers', 'destination')) {
                $table->string('destination')->nullable();
            }
            
            // Add description column
            if (!Schema::hasColumn('transfers', 'description')) {
                $table->text('description')->nullable();
            }
            
            // Add admin_notes column
            if (!Schema::hasColumn('transfers', 'admin_notes')) {
                $table->text('admin_notes')->nullable();
            }
            
            // Add processed_at timestamp
            if (!Schema::hasColumn('transfers', 'processed_at')) {
                $table->timestamp('processed_at')->nullable();
            }
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('transfers', function (Blueprint $table) {
            if (Schema::hasColumn('transfers', 'processed_at')) {
                $table->dropColumn('processed_at');
            }
            if (Schema::hasColumn('transfers', 'admin_notes')) {
                $table->dropColumn('admin_notes');
            }
            if (Schema::hasColumn('transfers', 'description')) {
                $table->dropColumn('description');
            }
            if (Schema::hasColumn('transfers', 'destination')) {
                $table->dropColumn('destination');
            }
            if (Schema::hasColumn('transfers', 'amount')) {
                $table->dropColumn('amount');
            }
            if (Schema::hasColumn('transfers', 'recipient_id')) {
                $table->dropForeign(['recipient_id']);
                $table->dropColumn('recipient_id');
            }
            if (Schema::hasColumn('transfers', 'sender_id')) {
                $table->dropForeign(['sender_id']);
                $table->dropColumn('sender_id');
            }
        });
    }
};
