<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('menu_groups', function (Blueprint $table) {
            $table->dropUnique(['name']);
            $table->foreignId('tenant_id')
                ->nullable()
                ->after('id')
                ->constrained()
                ->cascadeOnDelete();
            $table->boolean('is_system')->default(false)->after('description');
            $table->unique(['tenant_id', 'name']);
        });

        DB::table('menu_groups')
            ->whereNull('tenant_id')
            ->update(['is_system' => true]);
    }

    public function down(): void
    {
        Schema::table('menu_groups', function (Blueprint $table) {
            $table->dropUnique(['tenant_id', 'name']);
            $table->dropConstrainedForeignId('tenant_id');
            $table->dropColumn('is_system');
            $table->unique('name');
        });
    }
};
