<?php
/**
 * Migração: cria tabela latency_events
 * Execute uma vez: php patch_latency_events.php
 */
require_once __DIR__ . '/db.php';

$pdo = db();

$pdo->exec("
    CREATE TABLE IF NOT EXISTS latency_events (
        id             BIGSERIAL PRIMARY KEY,
        monitored_ip_id INTEGER NOT NULL REFERENCES monitored_ips(id) ON DELETE CASCADE,
        latency_ms     INTEGER NOT NULL,
        threshold_ms   INTEGER NOT NULL,
        occurred_at    TIMESTAMPTZ NOT NULL DEFAULT NOW()
    );

    CREATE INDEX IF NOT EXISTS idx_latency_events_ip_at
        ON latency_events(monitored_ip_id, occurred_at DESC);
");

echo "Tabela latency_events criada com sucesso.\n";
