<?php

declare(strict_types=1);

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('dberp_user_permission_grants', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id');
            $table->foreign('user_id', 'fk_dberp_user_perm_user')->references('id')->on('users')->cascadeOnDelete();
            $table->string('permission_code', 64)->comment('权限点');
            $table->foreignId('granted_by')->nullable();
            $table->foreign('granted_by', 'fk_dberp_user_perm_granted_by')->references('id')->on('users')->nullOnDelete();
            $table->string('remark', 255)->nullable()->comment('备注');
            $table->timestamps();

            $table->unique(['user_id', 'permission_code'], 'uq_dberp_user_permission_grant');
            $table->index(['permission_code', 'user_id'], 'idx_dberp_user_permission_lookup');
        });
    }

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