<?php
/**
 * View — Diagnostic Ralentissement (généraliste).
 *
 * @var string|null $fetchError
 * @var array       $topWaits
 * @var array       $activeSess
 * @var array       $topSql
 * @var array       $longOps
 * @var Closure     $fmtN
 * @var Closure     $wcClass
 */
?>

<div class="page-header" style="display:flex;align-items:center;flex-wrap:wrap;gap:.5rem">
    <h1 style="margin:0;font-size:1.05rem">🩺 <?= t('demo_oracle.diag_slowdown_title') ?></h1>
</div>
<p style="font-size:.85rem;color:var(--color-text-muted);margin:.25rem 0 .75rem">
    <?= t('demo_oracle.diag_slowdown_hint') ?>
</p>

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

<!-- ══ Top wait events ═══════════════════════════════════════════════════ -->
<?php if (!empty($topWaits)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_slowdown_top_waits') ?> (V$SYSTEM_EVENT)
</h2>
<div class="card" style="padding:0;overflow-x:auto;margin-bottom:.75rem">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">event</th>
            <th style="text-align:left;padding:.3rem .5rem">wait_class</th>
            <th style="text-align:right;padding:.3rem .5rem">total_waits</th>
            <th style="text-align:right;padding:.3rem .5rem">time_waited (s)</th>
            <th style="text-align:right;padding:.3rem .5rem">avg (ms)</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($topWaits as $w): ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.78rem"><?= htmlspecialchars($w['EVENT'] ?? '') ?></td>
            <td style="padding:.3rem .5rem">
                <span class="badge" style="font-size:.7rem;<?= $wcClass((string)($w['WAIT_CLASS'] ?? '')) ?>">
                    <?= htmlspecialchars($w['WAIT_CLASS'] ?? '') ?>
                </span>
            </td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtN($w['TOTAL_WAITS']  ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right;font-weight:600"><?= htmlspecialchars($fmtN($w['TIME_WAITED_S'] ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($w['AVG_MS']       ?? '0.00') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ══ Sessions actives (instantané) ════════════════════════════════════ -->
<?php if (!empty($activeSess)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_slowdown_active_sessions') ?> (<?= count($activeSess) ?>)
    <small style="font-weight:400;color:var(--color-text-muted)">
        — <a href="<?= htmlspecialchars(demo_oracle_url('report', ['section' => 'realtime/sessions'])) ?>"><?= t('demo_oracle.diag_slowdown_active_link') ?></a>
    </small>
</h2>
<div class="card" style="padding:0;overflow-x:auto;margin-bottom:.75rem">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">sid</th>
            <th style="text-align:left;padding:.3rem .5rem">user</th>
            <th style="text-align:left;padding:.3rem .5rem">sql_id</th>
            <th style="text-align:right;padding:.3rem .5rem">last_call (s)</th>
            <th style="text-align:left;padding:.3rem .5rem">wait_class</th>
            <th style="text-align:left;padding:.3rem .5rem">event</th>
            <th style="text-align:left;padding:.3rem .5rem">machine / program</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($activeSess as $s):
        $sqlId = (string)($s['SQL_ID'] ?? '');
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><code><?= (int)$s['INST_ID'] ?>:<?= (int)$s['SID'] ?>,<?= (int)$s['SERIAL_NUM'] ?></code></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($s['USERNAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem">
                <?php if ($sqlId !== ''): ?>
                <a href="<?= htmlspecialchars(demo_oracle_url('report', ['section' => 'sql/detail']) . '&sql_id=' . rawurlencode($sqlId)) ?>" target="_blank">
                    <code><?= htmlspecialchars($sqlId) ?></code>
                </a>
                <?php else: ?>—<?php endif; ?>
            </td>
            <td style="padding:.3rem .5rem;text-align:right"><?= (int)($s['LAST_CALL_ET'] ?? 0) ?></td>
            <td style="padding:.3rem .5rem">
                <span class="badge" style="font-size:.7rem;<?= $wcClass((string)($s['WAIT_CLASS'] ?? '')) ?>">
                    <?= htmlspecialchars($s['WAIT_CLASS'] ?? '') ?>
                </span>
            </td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.74rem"><?= htmlspecialchars($s['EVENT']   ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-size:.76rem"><?= htmlspecialchars(($s['MACHINE'] ?? '') . ' / ' . ($s['PROGRAM'] ?? '')) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ══ Top SQL ══════════════════════════════════════════════════════════ -->
<?php if (!empty($topSql)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_slowdown_top_sql') ?> (V$SQLSTATS)
</h2>
<div class="card" style="padding:0;overflow-x:auto;margin-bottom:.75rem">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">sql_id</th>
            <th style="text-align:left;padding:.3rem .5rem">schema</th>
            <th style="text-align:right;padding:.3rem .5rem">exec</th>
            <th style="text-align:right;padding:.3rem .5rem">elapsed (s)</th>
            <th style="text-align:right;padding:.3rem .5rem">cpu (s)</th>
            <th style="text-align:right;padding:.3rem .5rem">buffer_gets</th>
            <th style="text-align:right;padding:.3rem .5rem">disk_reads</th>
            <th style="text-align:left;padding:.3rem .5rem">preview</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($topSql as $q):
        $sqlId = (string)($q['SQL_ID'] ?? '');
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem">
                <?php if ($sqlId !== ''): ?>
                <a href="<?= htmlspecialchars(demo_oracle_url('report', ['section' => 'sql/detail']) . '&sql_id=' . rawurlencode($sqlId)) ?>" target="_blank">
                    <code><?= htmlspecialchars($sqlId) ?></code>
                </a>
                <?php endif; ?>
            </td>
            <td style="padding:.3rem .5rem;font-size:.78rem"><?= htmlspecialchars($q['PARSING_SCHEMA_NAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtN($q['EXECUTIONS']    ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right;font-weight:600"><?= htmlspecialchars($fmtN($q['ELAPSED_S']     ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtN($q['CPU_S']         ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtN($q['BUFFER_GETS']   ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtN($q['DISK_READS']    ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.74rem;max-width:340px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">
                <?= htmlspecialchars($q['SQL_PREVIEW'] ?? '') ?>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- ══ Long operations ══════════════════════════════════════════════════ -->
<?php if (!empty($longOps)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_slowdown_longops') ?> (V$SESSION_LONGOPS)
</h2>
<div class="card" style="padding:0;overflow-x:auto">
<table class="data-table" style="width:100%;border-collapse:collapse;font-size:.82rem">
    <thead>
        <tr style="background:var(--color-input-bg,#f8f8f8);border-bottom:1px solid var(--color-border)">
            <th style="text-align:left;padding:.3rem .5rem">sid</th>
            <th style="text-align:left;padding:.3rem .5rem">user</th>
            <th style="text-align:left;padding:.3rem .5rem">opname</th>
            <th style="text-align:left;padding:.3rem .5rem">target</th>
            <th style="text-align:right;padding:.3rem .5rem">progress</th>
            <th style="text-align:right;padding:.3rem .5rem">elapsed (s)</th>
            <th style="text-align:right;padding:.3rem .5rem">remaining (s)</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($longOps as $lo):
        $total = (float)($lo['TOTALWORK'] ?? 0);
        $sofar = (float)($lo['SOFAR']     ?? 0);
        $pct   = $total > 0 ? round($sofar * 100 / $total, 1) : 0;
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><code><?= (int)$lo['INST_ID'] ?>:<?= (int)$lo['SID'] ?>,<?= (int)$lo['SERIAL_NUM'] ?></code></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($lo['USERNAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($lo['OPNAME']   ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-size:.76rem"><?= htmlspecialchars($lo['TARGET'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= $pct ?> %</td>
            <td style="padding:.3rem .5rem;text-align:right"><?= (int)($lo['ELAPSED_SECONDS'] ?? 0) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= (int)($lo['TIME_REMAINING']  ?? 0) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<div class="alert alert-info" style="margin-top:.75rem;font-size:.84rem;line-height:1.5">
    <strong><?= t('demo_oracle.diag_slowdown_next') ?></strong>
    <ul style="margin:.25rem 0 0 1.25rem">
        <li><a href="<?= htmlspecialchars(demo_oracle_url('report', ['section' => 'history'])) ?>"><?= t('demo_oracle.report_history') ?></a> — <?= t('demo_oracle.diag_slowdown_history_hint') ?></li>
        <li><a href="<?= htmlspecialchars(demo_oracle_url('report', ['section' => 'realtime/locks'])) ?>"><?= t('demo_oracle.locks_title') ?></a> — <?= t('demo_oracle.diag_slowdown_locks_hint') ?></li>
        <li><a href="<?= htmlspecialchars(demo_oracle_url('report', ['section' => 'config/indicators'])) ?>"><?= t('demo_oracle.indicators_title') ?></a> — <?= t('demo_oracle.diag_slowdown_indicators_hint') ?></li>
    </ul>
</div>
