<?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::table('roles', function (Blueprint $table): void {
            $table->boolean('is_active')->default(true)->after('description');
            $table->softDeletes();
            $table->index(['tenant_id', 'is_active', 'deleted_at'], 'roles_tenant_lifecycle_idx');
        });

        Schema::table('users', function (Blueprint $table): void {
            $table->boolean('is_active')->default(true)->after('is_tenant_owner');
            $table->timestamp('deactivated_at')->nullable()->after('is_active');
            $table->index(['tenant_id', 'is_active'], 'users_tenant_active_idx');
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table): void {
            $table->dropIndex('users_tenant_active_idx');
            $table->dropColumn(['is_active', 'deactivated_at']);
        });

        Schema::table('roles', function (Blueprint $table): void {
            $table->dropIndex('roles_tenant_lifecycle_idx');
            $table->dropColumn(['is_active', 'deleted_at']);
        });
    }
};
