<?php

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('tenant_settings', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('workspace_id')->constrained()->cascadeOnDelete();
            $table->foreignId('managed_environment_id');
            $table->string('domain');
            $table->string('key');
            $table->json('value');
            $table->foreignId('updated_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->unique(['managed_environment_id', 'domain', 'key']);
            $table->index(['workspace_id', 'managed_environment_id']);

            $table->foreign(['managed_environment_id', 'workspace_id'], 'tenant_settings_managed_environment_workspace_fk')
                ->references(['id', 'workspace_id'])
                ->on('managed_environments')
                ->cascadeOnDelete();
        });
    }

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