<?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('alert_rules', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('workspace_id')->constrained()->cascadeOnDelete();
            $table->string('name');
            $table->boolean('is_enabled')->default(true);
            $table->string('event_type');
            $table->string('minimum_severity');
            $table->string('tenant_scope_mode')->default('all');
            $table->json('tenant_allowlist')->nullable();
            $table->unsignedInteger('cooldown_seconds')->nullable();
            $table->boolean('quiet_hours_enabled')->default(false);
            $table->string('quiet_hours_start')->nullable();
            $table->string('quiet_hours_end')->nullable();
            $table->string('quiet_hours_timezone')->nullable();
            $table->timestamps();

            $table->index(['workspace_id', 'event_type']);
            $table->index(['workspace_id', 'is_enabled']);
            $table->index(['workspace_id', 'tenant_scope_mode']);
        });
    }

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