<?php
/**
 * View: Demo Oracle — historique d'activite.
 *
 * Portage de modules/oracle/views/report/history.php. Courbe et cartes de
 * chaleur identiques ; les trois profondeurs restent affichees, deux
 * desactivees, la valeur servie etant figee cote serveur.
 *
 * View — Oracle historical activity report (ASH timeline + heatmaps).
 *
 * @var string|null $fetchError
 * @var bool        $hasLicense
 * @var string      $packAccess
 * @var string      $dataSource     'bash_snapper' | 'diag_pack' | ''
 * @var int         $days
 * @var array       $ashData        [{sample_bucket, category, cnt}, …]
 * @var array       $heatChargeData [{day, hour, cnt}, …]
 * @var array       $heatIoData     [{day, hour, cnt}, …]
 * @var array       $heatLogData    [{day, hour, cnt}, …]
 * @var array       $oraDb
 * @var object      $settings
 */
?>

<div class="page-header" style="display:flex;align-items:center;flex-wrap:wrap;gap:.75rem;margin-bottom:.35rem">
    <h1 style="margin:0;flex-shrink:0">📅 <?= t('demo_oracle.ash_title') ?></h1>
    <?php if (($dataSource ?? '') === 'bash_snapper'): ?>
    <span class="badge badge-warning" style="font-size:.72rem"
          title="<?= t('demo_oracle.bash_install_link') ?>">sw$bash_snapper</span>
    <?php endif; ?>
    <?php if (!$fetchError && $hasLicense): ?>
    <div style="display:flex;align-items:center;gap:.4rem;flex-wrap:wrap">
        <span style="font-size:.82rem;color:var(--color-text-muted)"><?= t('demo_oracle.ash_days_label') ?></span>
        <?php /* Les trois profondeurs du module complet restent affichees ;
                 deux sont eteintes. Les masquer laisserait croire que le choix
                 n'existe pas. La valeur servie est figee cote serveur : cliquer
                 sur « 30 j » par une URL forgee ne change rien. */ ?>
        <?php foreach ([1 => 'demo_oracle.ash_1d', 7 => 'demo_oracle.ash_7d', 30 => 'demo_oracle.ash_30d'] as $d => $key):
            $on = ($d === $days);
        ?>
        <button type="button" class="btn btn-sm <?= $on ? 'btn-primary' : 'btn-secondary' ?>"
                <?= $on ? '' : DemoOracleDemo::disabled() ?>><?= t($key) ?></button>
        <?php endforeach; ?>
        <?= DemoOracleDemo::badge(t('demo_oracle.history_days_hint', ['days' => (string)$days])) ?>
    </div>
    <?php endif; ?>
</div>

<?php if ($fetchError): ?>
<div class="alert alert-error"><?= htmlspecialchars($fetchError) ?></div>

<?php elseif (!$hasLicense): ?>
<div class="card">
    <div class="alert alert-warning" style="margin:0">
        🔒 <?= t('demo_oracle.ash_no_license') ?>
        <br><code style="font-size:.82em">CONTROL_MANAGEMENT_PACK_ACCESS = <?= htmlspecialchars($packAccess ?: 'NONE') ?></code>
        <?php /* Le module complet propose ici, aux profils DBA, un lien vers
                 l'installation de sw$bash_snapper. La demonstration ne sait
                 rien deployer : elle ne l'affiche pas. */ ?>
    </div>
</div>

<?php else: ?>

<!-- ══════════════════════════════════════════════════════════
     PART 1 — ASH stacked bar chart
══════════════════════════════════════════════════════════ -->
<div class="card">
    <div style="display:flex;align-items:center;gap:.75rem;margin-bottom:.25rem">
        <h2 style="margin:0;flex-shrink:0;font-size:1rem"><?= t('demo_oracle.ash_chart_title') ?></h2>
        <div id="chart-ash-legend" style="flex:1;min-width:0;overflow-x:auto"></div>
    </div>
    <?php if (empty($ashData)): ?>
    <p style="color:var(--color-text-muted)"><?= t('demo_oracle.ash_no_data') ?></p>
    <?php else: ?>
    <div style="overflow-x:auto"><div class="ora-chart-container" id="chart-ash"></div></div>
    <?php endif; ?>
</div>

<!-- ══════════════════════════════════════════════════════════
     PART 1b — Heatmaps (Charge / I/O / Application)
══════════════════════════════════════════════════════════ -->
<div class="card" style="margin-top:.5rem">
    <p style="color:var(--color-text-muted);font-size:.78rem;margin:0 0 .7rem"><?= t('demo_oracle.heat_hint') ?></p>
    <!-- Max 3 graphiques par ligne -->
    <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:1.5rem;align-items:start">
        <?php foreach ([
            ['key' => 'charge', 'id' => 'chart-heat-charge', 'title' => 'Charge',      'data' => $heatChargeData],
            ['key' => 'io',     'id' => 'chart-heat-io',     'title' => 'I/O',         'data' => $heatIoData],
            ['key' => 'log',    'id' => 'chart-heat-log',    'title' => 'Application', 'data' => $heatLogData],
        ] as $_hm): ?>
        <div style="min-width:0">
            <h3 style="margin:0 0 .35rem;font-size:.85rem"><?= $_hm['title'] ?></h3>
            <?php if (empty($_hm['data'])): ?>
            <p style="color:var(--color-text-muted);font-size:.85rem"><?= t('demo_oracle.ash_no_data') ?></p>
            <?php else: ?>
            <?php /* Une légende par heatmap : les trois rampes de couleur
                     diffèrent et chaque grille a son propre haut de domaine —
                     la légende commune d'avant ne pouvait en décrire qu'une.
                     Remplie par report-history.js. */ ?>
            <div id="heat-legend-<?= $_hm['key'] ?>"
                 style="display:flex;align-items:center;font-size:.72rem;margin-bottom:.3rem"></div>
            <div class="ora-heatmap-host"><div id="<?= $_hm['id'] ?>"></div></div>
            <?php endif; ?>
        </div>
        <?php endforeach; unset($_hm); ?>
    </div>
</div>

<?php endif; // end hasLicense ?>

<!-- ══ CDN + data bridge + external JS ═════════════════════════════════ -->
<script src="<?= htmlspecialchars($settings->get('cdn_d3_js', '')) ?>"></script>
<script>
window.DemoOracleReport = window.DemoOracleReport || {};
window.DemoOracleReport.history = {
    ashData:        <?= json_encode($ashData) ?>,
    days:           <?= $days ?>,
    yLabel:         <?= json_encode(t('demo_oracle.ash_y_label')) ?>,
    heatChargeData: <?= json_encode($heatChargeData) ?>,
    heatIoData:     <?= json_encode($heatIoData) ?>,
    heatLogData:    <?= json_encode($heatLogData) ?>,
    detailBaseUrl:  <?= json_encode(demo_oracle_url('report', ['section' => 'history/detail'])) ?>,
    i18n: {
        heatScaleLin:  <?= json_encode(t('demo_oracle.heat_scale_lin')) ?>,
        heatScaleLog:  <?= json_encode(t('demo_oracle.heat_scale_log')) ?>,
        heatScaleAuto: <?= json_encode(t('demo_oracle.heat_scale_auto')) ?>,
        heatScaleHint: <?= json_encode(t('demo_oracle.heat_scale_hint')) ?>,
        heatClipped:   <?= json_encode(t('demo_oracle.heat_clipped')) ?>,
        heatMax:       <?= json_encode(t('demo_oracle.heat_max')) ?>
    }
};
</script>
<script src="/modules/demo_oracle/assets/js/report-history.js?v=<?= filemtime(__DIR__ . '/../../assets/js/report-history.js') ?>"></script>
