<?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('alert_rules', function (Blueprint $table) {
            if (! Schema::hasColumn('alert_rules', 'notification_emails')) {
                $table->json('notification_emails')->nullable()->after('notification_channels');
            }

            if (! Schema::hasColumn('alert_rules', 'cooldown_minutes')) {
                $table->unsignedInteger('cooldown_minutes')->default(1440)->after('notification_emails');
            }

            if (! Schema::hasColumn('alert_rules', 'last_evaluated_at')) {
                $table->timestamp('last_evaluated_at')->nullable()->after('cooldown_minutes');
            }

            if (! Schema::hasColumn('alert_rules', 'last_triggered_at')) {
                $table->timestamp('last_triggered_at')->nullable()->after('last_evaluated_at');
            }

            if (! Schema::hasColumn('alert_rules', 'last_value')) {
                $table->decimal('last_value', 18, 6)->nullable()->after('last_triggered_at');
            }

            if (! Schema::hasColumn('alert_rules', 'last_status')) {
                $table->string('last_status', 30)->nullable()->after('last_value');
            }

            if (! Schema::hasColumn('alert_rules', 'meta')) {
                $table->json('meta')->nullable()->after('last_status');
            }
        });

        Schema::table('alert_logs', function (Blueprint $table) {
            if (! Schema::hasColumn('alert_logs', 'metric')) {
                $table->string('metric', 120)->nullable()->after('severity');
            }

            if (! Schema::hasColumn('alert_logs', 'operator')) {
                $table->string('operator', 20)->nullable()->after('metric');
            }

            if (! Schema::hasColumn('alert_logs', 'threshold')) {
                $table->decimal('threshold', 18, 6)->nullable()->after('operator');
            }

            if (! Schema::hasColumn('alert_logs', 'current_value')) {
                $table->decimal('current_value', 18, 6)->nullable()->after('threshold');
            }

            if (! Schema::hasColumn('alert_logs', 'period')) {
                $table->string('period', 30)->nullable()->after('current_value');
            }

            if (! Schema::hasColumn('alert_logs', 'period_start')) {
                $table->date('period_start')->nullable()->after('period');
            }

            if (! Schema::hasColumn('alert_logs', 'period_end')) {
                $table->date('period_end')->nullable()->after('period_start');
            }

            if (! Schema::hasColumn('alert_logs', 'status')) {
                $table->string('status', 30)->default('open')->after('period_end');
            }

            if (! Schema::hasColumn('alert_logs', 'fingerprint')) {
                $table->string('fingerprint', 64)->nullable()->after('status');
                $table->index('fingerprint');
            }

            if (! Schema::hasColumn('alert_logs', 'notified_at')) {
                $table->timestamp('notified_at')->nullable()->after('triggered_at');
            }

            $table->index(['status', 'triggered_at'], 'alert_logs_status_triggered_idx');
            $table->index(['client_id', 'status', 'triggered_at'], 'alert_logs_client_status_idx');
        });
    }

    public function down(): void
    {
        Schema::table('alert_logs', function (Blueprint $table) {
            if (Schema::hasColumn('alert_logs', 'fingerprint')) {
                $table->dropIndex(['fingerprint']);
            }
            $table->dropIndex('alert_logs_status_triggered_idx');
            $table->dropIndex('alert_logs_client_status_idx');

            foreach ([
                'metric', 'operator', 'threshold', 'current_value', 'period', 'period_start', 'period_end', 'status', 'fingerprint', 'notified_at',
            ] as $column) {
                if (Schema::hasColumn('alert_logs', $column)) {
                    $table->dropColumn($column);
                }
            }
        });

        Schema::table('alert_rules', function (Blueprint $table) {
            foreach ([
                'notification_emails', 'cooldown_minutes', 'last_evaluated_at', 'last_triggered_at', 'last_value', 'last_status', 'meta',
            ] as $column) {
                if (Schema::hasColumn('alert_rules', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
