<?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
    {
        Schema::table('transaction_receipt', function (Blueprint $table) {

            // Rename column
            $table->renameColumn('Usr_Id', 'Generated_By');
        });

        Schema::table('transaction_receipt', function (Blueprint $table) {

            // Add new column
            $table->unsignedBigInteger('Approved_By')->nullable()->after('Generated_By');

            // Foreign Keys
            $table->foreign('Generated_By')
                  ->references('Usr_Id')
                  ->on('user_master')
                  ->onDelete('cascade');

            $table->foreign('Approved_By')
                  ->references('Usr_Id')
                  ->on('user_master')
                  ->onDelete('set null');
        });
    }

    public function down(): void
    {
        Schema::table('transaction_receipt', function (Blueprint $table) {

            $table->dropForeign(['Generated_By']);
            $table->dropForeign(['Approved_By']);

            $table->dropColumn('Approved_By');

            $table->renameColumn('Generated_By', 'Usr_Id');
        });
    }
};
