<?php
// ── 1. Fix hostname em interface_dashboard.php ─────────────────────────────
$f1 = "/var/www/html/mikrotik/views/interface_dashboard.php";
$c1 = file_get_contents($f1);
$c1 = str_replace('nd.host AS device_host', 'nd.hostname AS device_host', $c1);
file_put_contents($f1, $c1);
echo (strpos($c1, 'nd.hostname AS device_host') !== false ? "OK fix hostname\n" : "FAIL hostname\n");

// ── 2. Adiciona secao "Interfaces Monitoradas" no dashboard.php ────────────
$f2 = "/var/www/html/mikrotik/views/dashboard.php";
$c2 = file_get_contents($f2);

// Ancora: antes do bloco de Alertas (ou antes do </div> final se nao tiver alertas)
$anchor = "    <!-- Alertas -->";
if (strpos($c2, 'Interfaces Monitoradas') !== false) {
    echo "SKIP dashboard.php (ja existe)\n";
} elseif (strpos($c2, $anchor) !== false) {

$block = <<<'PHPBLOCK'

    <!-- Interfaces Monitoradas -->
    <?php
    $monStmt = $pdo->prepare("
        SELECT ni.id, ni.if_name, ni.if_alias, ni.last_status,
               ni.last_in_bps, ni.last_out_bps, ni.alert_email,
               nd.name AS device_name, nd.id AS device_id
        FROM nms_interfaces ni
        JOIN nms_devices nd ON nd.id = ni.device_id
        WHERE nd.user_id = ? AND ni.alert_on_down = TRUE
        ORDER BY nd.name ASC, ni.if_name ASC
    ");
    $monStmt->execute([$user['id']]);
    $monIfaces = $monStmt->fetchAll();
    ?>
    <?php if (!empty($monIfaces)): ?>
    <div style="background:#fff;border-radius:16px;border:1px solid #e2e8f0;overflow:hidden;">
        <div style="padding:20px 24px;border-bottom:1px solid #e2e8f0;background:linear-gradient(135deg,#f0f9ff,#fff);display:flex;justify-content:space-between;align-items:center;">
            <h3 style="margin:0;font-size:1.05rem;font-weight:800;color:#0f172a;">🔔 Interfaces Monitoradas (<?= count($monIfaces) ?>)</h3>
        </div>
        <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:0;">
        <?php foreach ($monIfaces as $mi):
            $miUp   = ($mi['last_status'] ?? 'unknown') === 'up';
            $stBg   = $miUp ? '#f0fdf4' : '#fef2f2';
            $stBd   = $miUp ? '#86efac' : '#fca5a5';
            $stClr  = $miUp ? '#15803d' : '#dc2626';
            $stIcon = $miUp ? '🟢' : '🔴';
            $label  = h($mi['if_alias'] ?: $mi['if_name']);
            $dashUrl = h(NMS_URL . '?page=nms_interface_dashboard&id=' . (int)$mi['id']);
            $devUrl  = h(NMS_URL . '?page=nms_device_detail&id=' . (int)$mi['device_id']);
        ?>
            <div style="padding:18px 20px;border-bottom:1px solid #f1f5f9;border-right:1px solid #f1f5f9;display:flex;flex-direction:column;gap:10px;">
                <div style="display:flex;align-items:flex-start;justify-content:space-between;gap:8px;">
                    <div style="min-width:0;">
                        <div style="font-weight:800;font-size:0.95rem;color:#0f172a;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;" title="<?= $label ?>"><?= $label ?></div>
                        <div style="font-size:0.75rem;color:#94a3b8;margin-top:2px;">
                            <a href="<?= $devUrl ?>" style="color:#94a3b8;text-decoration:none;font-weight:600;"><?= h($mi['device_name']) ?></a>
                            · <span style="font-family:monospace;"><?= h($mi['if_name']) ?></span>
                        </div>
                    </div>
                    <span style="background:<?= $stBg ?>;border:1px solid <?= $stBd ?>;color:<?= $stClr ?>;font-size:0.78rem;font-weight:800;padding:4px 10px;border-radius:20px;white-space:nowrap;flex-shrink:0;">
                        <?= $stIcon ?> <?= $miUp ? 'Online' : 'Offline' ?>
                    </span>
                </div>
                <div style="display:flex;gap:14px;">
                    <div>
                        <div style="font-size:0.7rem;color:#94a3b8;font-weight:600;text-transform:uppercase;">↓ Entrada</div>
                        <div style="font-size:0.92rem;font-weight:800;color:#22c55e;"><?= format_bps((int)($mi['last_in_bps'] ?? 0)) ?></div>
                    </div>
                    <div>
                        <div style="font-size:0.7rem;color:#94a3b8;font-weight:600;text-transform:uppercase;">↑ Saída</div>
                        <div style="font-size:0.92rem;font-weight:800;color:#ef4444;"><?= format_bps((int)($mi['last_out_bps'] ?? 0)) ?></div>
                    </div>
                </div>
                <a href="<?= $dashUrl ?>"
                   style="display:inline-flex;align-items:center;justify-content:center;gap:6px;padding:8px 14px;background:#ede9fe;color:#4f46e5;border:1px solid #c4b5fd;border-radius:8px;font-size:0.82rem;font-weight:700;text-decoration:none;transition:background .15s;"
                   onmouseover="this.style.background='#ddd6fe'" onmouseout="this.style.background='#ede9fe'">
                    📊 Ver gráfico da interface
                </a>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

PHPBLOCK;

    $c2 = str_replace($anchor, $block . $anchor, $c2);
    file_put_contents($f2, $c2);
    echo "OK dashboard.php\n";
} else {
    echo "FAIL dashboard.php — ancora nao encontrada\n";
}
