<?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) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('name');
            $table->string('metric');                 // cpu|mem|disk|load|offline
            $table->string('comparator')->default('gt'); // gt|lt (ignored for offline)
            // decimal, not int: a % metric is 0-100 but `load` is a float (e.g. 1.5) — an int
            // threshold would truncate it so `load 1.5 > 1` never fires.
            $table->decimal('threshold', 8, 2)->default(0);
            $table->string('scope_type')->default('all'); // all|group|server
            $table->unsignedBigInteger('scope_id')->nullable();
            $table->boolean('enabled')->default(true);
            $table->timestamps();

            $table->index(['enabled', 'metric']);
        });
    }

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