<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('tenant_role_mappings', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignId('managed_environment_id')->constrained()->cascadeOnDelete();
            $table->enum('mapping_type', ['entra_group', 'entra_app_role']);
            $table->string('external_id');
            $table->enum('role', ['owner', 'manager', 'operator', 'readonly']);
            $table->boolean('is_enabled')->default(true);
            $table->timestamps();

            $table->unique(['managed_environment_id', 'mapping_type', 'external_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('tenant_role_mappings');
    }
};
