<?php
// ── 1. Adiciona rota nms_interface_dashboard no index.php ──────────────────
$f1 = "/var/www/html/mikrotik/index.php";
$c1 = file_get_contents($f1);
$old1 = "    case 'nms_api_realtime':";
$new1 = "    case 'nms_interface_dashboard':
        \$pdo = db();
        require __DIR__ . '/views/interface_dashboard.php';
        break;

    case 'nms_api_realtime':";
if (strpos($c1, 'nms_interface_dashboard') !== false) {
    echo "SKIP index.php (ja existe)\n";
} elseif (strpos($c1, $old1) !== false) {
    file_put_contents($f1, str_replace($old1, $new1, $c1));
    echo "OK index.php\n";
} else {
    echo "FAIL index.php — ancora nao encontrada\n";
}

// ── 2. Adiciona periodo 7d na API history.php ──────────────────────────────
$f2 = "/var/www/html/mikrotik/views/api/history.php";
$c2 = file_get_contents($f2);
$old2 = "\$intervals = ['5min' => [\"INTERVAL '5 minutes'\", 'H:i'], '1h' => [\"INTERVAL '1 hour'\", 'H:i'], '24h' => [\"INTERVAL '24 hours'\", 'd/m H:i']];";
$new2 = "\$intervals = ['5min' => [\"INTERVAL '5 minutes'\", 'H:i'], '1h' => [\"INTERVAL '1 hour'\", 'H:i'], '6h' => [\"INTERVAL '6 hours'\", 'H:i'], '24h' => [\"INTERVAL '24 hours'\", 'd/m H:i'], '7d' => [\"INTERVAL '7 days'\", 'd/m']];";
if (strpos($c2, "'7d'") !== false) {
    echo "SKIP history.php (7d ja existe)\n";
} elseif (strpos($c2, $old2) !== false) {
    file_put_contents($f2, str_replace($old2, $new2, $c2));
    echo "OK history.php\n";
} else {
    echo "FAIL history.php — linha intervals nao encontrada\n";
}

// ── 3. Adiciona botao Dashboard no device_detail.php ──────────────────────
// Adiciona botao 📊 ao lado do botao de alerta existente (versao "Alertando")
$f3 = "/var/www/html/mikrotik/views/device_detail.php";
$c3 = file_get_contents($f3);

$old3 = '                                    <?php if (!empty($iface[\'alert_on_down\']) && !empty($iface[\'alert_email\'])): ?>
                                        <button onclick="toggleAlertRow(<?= (int)$iface[\'id\'] ?>)" title="Monitoramento ativo"
                                            style="display:inline-flex;align-items:center;gap:6px;padding:8px 14px;background:#dcfce7;color:#15803d;border:1px solid #86efac;border-radius:8px;font-size:0.85rem;font-weight:700;cursor:pointer;">
                                            🔔 Alertando
                                        </button>
                                    <?php else: ?>';

$new3 = '                                    <?php if (!empty($iface[\'alert_on_down\']) && !empty($iface[\'alert_email\'])): ?>
                                        <div style="display:inline-flex;gap:6px;align-items:center;">
                                            <button onclick="toggleAlertRow(<?= (int)$iface[\'id\'] ?>)" title="Monitoramento ativo"
                                                style="display:inline-flex;align-items:center;gap:6px;padding:8px 14px;background:#dcfce7;color:#15803d;border:1px solid #86efac;border-radius:8px;font-size:0.85rem;font-weight:700;cursor:pointer;">
                                                🔔 Alertando
                                            </button>
                                            <a href="<?= NMS_URL ?>?page=nms_interface_dashboard&id=<?= (int)$iface[\'id\'] ?>"
                                                title="Ver dashboard da interface"
                                                style="display:inline-flex;align-items:center;gap:6px;padding:8px 14px;background:#ede9fe;color:#4f46e5;border:1px solid #c4b5fd;border-radius:8px;font-size:0.85rem;font-weight:700;text-decoration:none;">
                                                📊 Dashboard
                                            </a>
                                        </div>
                                    <?php else: ?>';

if (strpos($c3, 'nms_interface_dashboard') !== false) {
    echo "SKIP device_detail.php (botao ja existe)\n";
} elseif (strpos($c3, $old3) !== false) {
    file_put_contents($f3, str_replace($old3, $new3, $c3));
    echo "OK device_detail.php\n";
} else {
    echo "FAIL device_detail.php — ancora do botao nao encontrada\n";
}

echo "DONE\n";
