/**
 * Oracle Indicators report — Chart.js advisors + help modal.
 * Reads data from window.DemoOracleReport.indicators.
 */
(function () {
'use strict';

var PAGE = (window.DemoOracleReport || {}).indicators;
if (!PAGE) return;

var HELP_RATIO     = PAGE.helpRatio;
var HELP_RATIO_LBL = PAGE.helpRatioLbl;
var HELP_ADVISOR   = PAGE.helpAdvisor;
var ADVISORS       = PAGE.advisors;

/* ── Modale d'aide ─────────────────────────────────────────────────────── */
function showHelp(kind, key) {
    var h = (kind === 'ratio') ? HELP_RATIO[key] : HELP_ADVISOR[key];
    if (!h) return;
    document.getElementById('ind-modal-title').textContent =
        (kind === 'ratio') ? (HELP_RATIO_LBL[key] || key) : h.title;
    document.getElementById('ind-modal-def').textContent  = h.def;
    document.getElementById('ind-modal-goal').textContent = h.goal;
    var ul = document.getElementById('ind-modal-levers');
    ul.innerHTML = '';
    (h.levers || []).forEach(function (lv) {
        var li = document.createElement('li');
        li.textContent = lv;
        ul.appendChild(li);
    });
    document.getElementById('ind-modal').style.display = 'flex';
}
window.indHideModal = function (ev) {
    if (ev && ev.target.id !== 'ind-modal') return;
    document.getElementById('ind-modal').style.display = 'none';
};

document.querySelectorAll('.ind-help-btn').forEach(function (b) {
    b.addEventListener('click', function () { showHelp('ratio', b.dataset.key); });
});
document.querySelectorAll('.ind-adv-help-btn').forEach(function (b) {
    b.addEventListener('click', function () { showHelp('advisor', b.dataset.key); });
});

/* ── Charts advisors (Chart.js) ────────────────────────────────────────── */
function fmtMB(v) {
    return (v >= 1024) ? (v / 1024).toFixed(1) + ' GB' : Math.round(v) + ' MB';
}

function renderAdvisor(key, rows) {
    var el = document.getElementById('adv-' + key);
    if (!el || !rows.length) return;
    var labels = rows.map(function (r) { return fmtMB(r.sz); });
    var data   = rows.map(function (r) { return r.metric; });
    // Met en évidence le point factor==1 (taille courante)
    var pointBg = rows.map(function (r) {
        return Math.abs(r.size_factor - 1) < 0.001 ? '#dc2626' : '#2563eb';
    });
    var pointRadius = rows.map(function (r) {
        return Math.abs(r.size_factor - 1) < 0.001 ? 6 : 3;
    });
    var yLabel = (key === 'db_cache') ? 'physical reads'
              : (key === 'shared_pool') ? 'lc load time (s)'
              : (key === 'pga') ? 'cache hit %'
              : 'estd db time';

    new Chart(el, {
        type: 'line',
        data: {
            labels: labels,
            datasets: [{
                label:           yLabel,
                data:            data,
                borderColor:     '#2563eb',
                backgroundColor: 'rgba(37,99,235,.10)',
                tension:         0.25,
                pointBackgroundColor: pointBg,
                pointRadius:     pointRadius,
                fill:            true
            }]
        },
        options: {
            responsive: true, maintainAspectRatio: false,
            plugins: { legend: { display: false } },
            scales:  { y: { beginAtZero: false } }
        }
    });
}

Object.keys(ADVISORS).forEach(function (k) {
    renderAdvisor(k, ADVISORS[k] || []);
});

})();
