<?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('maestro_modules', function (Blueprint $table) {
            $table->id();
            $table->string('slug')->unique();
            $table->string('label');
            $table->boolean('is_enabled')->default(true);
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('maestro_navigation_nodes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('parent_id')
                ->nullable()
                ->constrained('maestro_navigation_nodes')
                ->cascadeOnDelete();
            $table->foreignId('module_id')
                ->nullable()
                ->constrained('maestro_modules')
                ->nullOnDelete();
            $table->unsignedInteger('sort_order')->default(0);
            $table->string('key')->unique();
            $table->string('label');
            $table->string('icon')->nullable();
            $table->string('route')->nullable();
            $table->string('permission')->nullable();
            $table->boolean('is_header')->default(false);
            $table->boolean('pinned_bottom')->default(false);
            $table->text('description')->nullable();
            $table->timestamps();

            $table->index(['parent_id', 'sort_order']);
        });
    }

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