<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if(!Schema::hasTable('available_user_roles')) {
            Schema::create('available_user_roles', function (Blueprint $table) {
                $table->id('id');
                $table->string('role');
                $table->unsignedInteger('rank')->default(0);
                $table->string('description');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('user_roles')) {
            Schema::create('user_roles', function (Blueprint $table) {
                $table->unsignedBigInteger('character_id')->unique();
                $table->string('role')->default('Guest');
                $table->unsignedInteger('rank')->default(0);
                $table->timestamps();

                $table->foreign('character_id', 'fk_user_roles_character_id')
                      ->references('character_id')
                      ->on('users')
                      ->cascadeOnDelete();
            });
        }

        if(!Schema::hasTable('user_permissions')) {
            Schema::create('user_permissions', function (Blueprint $table) {
                $table->id('id');
                $table->unsignedBigInteger('character_id')->default(0);
                $table->string('permission');
                $table->timestamps();

                $table->foreign('character_id', 'fk_user_permissions_character_id')
                      ->references('character_id')
                      ->on('users')
                      ->cascadeOnDelete();
            });
        }

        if(!Schema::hasTable('available_user_permissions')) {
            Schema::create('available_user_permissions', function (Blueprint $table) {
                $table->increments('id');
                $table->string('permission');
                $table->text('description');
                $table->timestamps();
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('user_roles');
        Schema::dropIfExists('user_permissions');
        Schema::dropIfExists('available_user_roles');
        Schema::dropIfExists('available_user_permissions');
    }
};
