<?php

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

return new class extends Migration
{
    public function up()
    {
        Schema::table('payment_methods', function (Blueprint $table) {
            $table->dropForeign('payment_methods_bank_id_foreign');
        });

        DB::statement('ALTER TABLE `payment_methods` CHANGE `bank_id` `origin_id` BIGINT(20) UNSIGNED NULL');

        Schema::table('payment_methods', function (Blueprint $table) {
            $table->unsignedBigInteger('destination_id')->nullable()->after('origin_id');

            $table->foreign('origin_id', 'payment_methods_origin_id_foreign')->references('id')->on('banks')->onUpdate('cascade')->onDelete('cascade');

            $table->foreign('destination_id', 'payment_methods_destination_id_foreign')->references('id')->on('banks')->onUpdate('cascade')->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::table('payment_methods', function (Blueprint $table) {
            $table->dropForeign('payment_methods_destination_id_foreign');
            $table->dropForeign('payment_methods_origin_id_foreign');
            $table->dropColumn('destination_id');
        });

        DB::statement('ALTER TABLE `payment_methods`CHANGE `origin_id` `bank_id` BIGINT(20) UNSIGNED NULL');

        Schema::table('payment_methods', function (Blueprint $table) {
            $table->foreign('bank_id', 'payment_methods_bank_id_foreign')->references('id')->on('banks')->onUpdate('cascade')->onDelete('cascade');
        });
    }
};
