<?php
$f = "/var/www/html/mikrotik/views/dashboard.php";
$c = file_get_contents($f);

// Parte 1: substitui div do grafico
$old1 = 'height:100px;">
                    <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                    <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
                </div>
            </div>';

// tambem tenta versao sem a legenda de cima
$old1b = 'height:90px;">
                <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
            </div>';

$new1 = 'height:140px;">
                    <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                    <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
                </div>
            </div>';

// encontra e substitui bloco completo do mini grafico
// busca pelo padding do container do grafico
$patterns = [
    'padding:10px 20px 0',
    'padding:12px 20px 8px',
];

$found = false;
foreach ($patterns as $pat) {
    $pos = strpos($c, $pat);
    if ($pos !== false) {
        // encontra o inicio da div pai
        $divStart = strrpos(substr($c, 0, $pos), '<div');
        // encontra o bloco completo (ate fechar o div do canvas + 2 divs)
        // substitui tudo entre o comentario e o proximo div de botao
        $commentPos = strrpos(substr($c, 0, $pos), '<!-- ');
        if ($commentPos !== false) {
            $afterComment = substr($c, $commentPos);
            // encontra fim do bloco (<!-- Ultimo evento -->)
            $endPos = strpos($afterComment, '<!-- ');
            if ($endPos === false) $endPos = strpos($afterComment, '<!-- Bot');
            if ($endPos !== false) {
                $oldBlock = substr($afterComment, 0, $endPos);
                $newBlock = '<!-- Gr' . "\xc3\xa1" . 'fico de tr' . "\xc3\xa1" . 'fego -->
            <div style="padding:10px 20px 8px;">
                <div style="font-size:0.7rem;font-weight:700;color:#64748b;text-transform:uppercase;letter-spacing:.05em;margin-bottom:6px;">&#x1F4C8; Tr' . "\xc3\xa1" . 'fego &mdash; ' . "\'\'ltima 1h</div>" . '
                <div style="position:relative;height:140px;">
                    <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                    <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
                </div>
            </div>

            ';
                $c = substr($c, 0, $commentPos) . $newBlock . substr($c, $commentPos + $endPos);
                $found = true;
                echo "BLOCK OK\n";
                break;
            }
        }
    }
}

if (!$found) {
    echo "WARN: block not replaced, trying simple height replace\n";
}

// Parte 2: substitui o JS do grafico (options)
$old2 = "plugins: { legend: { display: false }, tooltip: { enabled: false } },
                    scales: {
                        x: { display: false },
                        y: { display: false, beginAtZero: true }
                    }";

$new2 = "interaction: { mode: 'index', intersect: false },
                    plugins: {
                        legend: {
                            display: true,
                            position: 'top',
                            labels: { font: { size: 10, weight: '700' }, padding: 8, boxWidth: 10, boxHeight: 2 }
                        },
                        tooltip: {
                            callbacks: { label: ctx => ' ' + ctx.dataset.label + ': ' + formatBps(ctx.parsed.y) }
                        }
                    },
                    scales: {
                        x: {
                            display: true,
                            ticks: { maxTicksLimit: 5, font: { size: 9 }, color: '#94a3b8', maxRotation: 0 },
                            grid: { display: false }
                        },
                        y: {
                            display: true,
                            beginAtZero: true,
                            ticks: { maxTicksLimit: 4, font: { size: 9 }, color: '#94a3b8', callback: v => formatBps(v) },
                            grid: { color: 'rgba(0,0,0,.04)' }
                        }
                    }";

if (strpos($c, $old2) !== false) {
    $c = str_replace($old2, $new2, $c);
    echo "JS OK\n";
} else {
    echo "WARN: JS already updated or not found\n";
}

// Parte 3: adiciona label nos datasets
$old3 = "{ data: data.in_bps,  borderColor: '#22c55e', borderWidth: 1.5, pointRadius: 0, fill: true, backgroundColor: 'rgba(34,197,94,0.07)', tension: 0.4 },
                        { data: data.out_bps, borderColor: '#ef4444', borderWidth: 1.5, pointRadius: 0, fill: true, backgroundColor: 'rgba(239,68,68,0.05)',  tension: 0.4 }";

$new3 = "{ label: '\u2193 Entrada', data: data.in_bps,  borderColor: '#22c55e', borderWidth: 2, pointRadius: 0, fill: true, backgroundColor: 'rgba(34,197,94,0.08)', tension: 0.3 },
                        { label: '\u2191 Sa\u00edda',   data: data.out_bps, borderColor: '#ef4444', borderWidth: 2, pointRadius: 0, fill: true, backgroundColor: 'rgba(239,68,68,0.06)',  tension: 0.3 }";

if (strpos($c, $old3) !== false) {
    $c = str_replace($old3, $new3, $c);
    echo "DATASETS OK\n";
} else {
    // tenta versao ja com label
    if (strpos($c, "label: '\u2193") !== false || strpos($c, "label: '\\u2193") !== false) {
        echo "DATASETS already updated\n";
    } else {
        echo "WARN: datasets not found\n";
    }
}

file_put_contents($f, $c);
echo "DONE\n";
