<?php
// Cria tabela nms_interface_events
require "/var/www/html/db.php";
$pdo = db();

$pdo->exec("
    CREATE TABLE IF NOT EXISTS nms_interface_events (
        id               BIGSERIAL PRIMARY KEY,
        interface_id     INT NOT NULL REFERENCES nms_interfaces(id) ON DELETE CASCADE,
        started_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
        recovered_at     TIMESTAMPTZ NULL,
        duration_seconds INT NULL,
        alert_sent       BOOLEAN NOT NULL DEFAULT FALSE
    )
");
echo "TABLE OK\n";

$pdo->exec("CREATE INDEX IF NOT EXISTS idx_nie_iface ON nms_interface_events(interface_id, started_at DESC)");
echo "INDEX 1 OK\n";

$pdo->exec("CREATE INDEX IF NOT EXISTS idx_nie_open ON nms_interface_events(interface_id, recovered_at) WHERE recovered_at IS NULL");
echo "INDEX 2 OK\n";

echo "DONE\n";
