<?php
/*=========================================================================================
    App Name  : PPID PTUN Kupang
    File Name : users
    --------------------------------------------------------------------------------------
    Author    : Fuad Auliya Rohman
    Email     : fuad.dev16@gmail.com
==========================================================================================*/

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::disableForeignKeyConstraints();
        Schema::table('users', function (Blueprint $table) {
            $table->bigInteger('created_user_id')->nullable()->unsigned()->after('deleted_at');
            $table->foreign('created_user_id')->references('id')->on('users')->onDelete('restrict')->onUpdate('cascade');

            $table->bigInteger('updated_user_id')->nullable()->unsigned()->after('created_user_id');
            $table->foreign('updated_user_id')->references('id')->on('users')->onDelete('restrict')->onUpdate('cascade');
        });
        Schema::enableForeignKeyConstraints();
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign('user_created_user_id_foreign');
            $table->dropForeign('user_updated_user_id_foreign');
            $table->dropColumn(['created_user_id', 'updated_user_id']);
        });
        Schema::enableForeignKeyConstraints();
    }
};
