<?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::create('permissions', function (Blueprint $table) {
            $table->id();
            $table->string('scope')->unique();
            $table->string('description');
            $table->integer('bits');
            $table->unsignedBigInteger('parent_id')->nullable();
            $table->foreign('parent_id')->references('id')->on('permissions')->onDelete('cascade');
            $table->integer('_lft');
            $table->integer('_rgt');
            $table->timestamps();
            $table->softDeletes();
            $table->index(['_lft', '_rgt']);
        });

        Schema::create('user_type_permissions', function (Blueprint $table) {
            $table->id();
            $table->string('user_type');
            $table->unsignedBigInteger('permission_id');
            $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
            $table->integer('bits');
            $table->timestamps();
            $table->softDeletes();
            $table->index(['user_type', 'permission_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('user_type_permissions');
        Schema::dropIfExists('permissions');
    }
};
