<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Kylin\Rbac\Enums\GuardName;

return new class extends Migration
{
    public function up(): void
    {
        $tableNames = config('kylin-rbac.table_names', [
            'roles' => 'kylin_roles',
            'permissions' => 'kylin_permissions',
            'role_has_permissions' => 'kylin_role_has_permissions',
            'model_has_roles' => 'kylin_model_has_roles',
            'model_has_permissions' => 'kylin_model_has_permissions',
        ]);

        Schema::create($tableNames['permissions'], function (Blueprint $table) {
            $table->id();
            $table->string('name', 125);
            $table->string('display_name', 125)->nullable()->comment('显示名称');
            $table->string('guard_name', 25);
            $table->string('module', 50)->nullable()->comment('所属模块');
            $table->string('description', 255)->nullable()->comment('描述');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['name', 'guard_name']);
        });

        Schema::create($tableNames['roles'], function (Blueprint $table) {
            $table->id();
            $table->string('name', 125);
            $table->string('display_name', 125)->nullable()->comment('显示名称');
            $table->string('guard_name', 25);
            $table->string('description', 255)->nullable()->comment('描述');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['name', 'guard_name']);
        });

        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
            $table->unsignedBigInteger('permission_id');
            $table->unsignedBigInteger('role_id');

            $table->foreign('permission_id')
                ->references('id')
                ->on($tableNames['permissions'])
                ->onDelete('cascade');

            $table->foreign('role_id')
                ->references('id')
                ->on($tableNames['roles'])
                ->onDelete('cascade');

            $table->primary(['permission_id', 'role_id']);
        });

        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames) {
            $table->unsignedBigInteger('permission_id');
            $table->string('model_type', 125);
            $table->unsignedBigInteger('model_id');

            $table->foreign('permission_id')
                ->references('id')
                ->on($tableNames['permissions'])
                ->onDelete('cascade');

            $table->index(['model_type', 'model_id'], 'model_has_permissions_model_id_model_type_index');

            $table->primary(
                ['permission_id', 'model_type', 'model_id'],
                'model_has_permissions_permission_model_type_primary'
            );
        });

        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames) {
            $table->unsignedBigInteger('role_id');
            $table->string('model_type', 125);
            $table->unsignedBigInteger('model_id');

            $table->foreign('role_id')
                ->references('id')
                ->on($tableNames['roles'])
                ->onDelete('cascade');

            $table->index(['model_type', 'model_id'], 'model_has_roles_model_id_model_type_index');

            $table->primary(
                ['role_id', 'model_type', 'model_id'],
                'model_has_roles_role_model_type_primary'
            );
        });
    }

    public function down(): void
    {
        $tableNames = config('kylin-rbac.table_names');
        Schema::dropIfExists($tableNames['model_has_permissions']);
        Schema::dropIfExists($tableNames['model_has_roles']);
        Schema::dropIfExists($tableNames['role_has_permissions']);
        Schema::dropIfExists($tableNames['permissions']);
        Schema::dropIfExists($tableNames['roles']);
    }
};
