<?php
/**
 * View — Diagnostic ORA-01555 « Snapshot too old ».
 *
 * @var string|null $fetchError
 * @var array       $params
 * @var array       $rows
 * @var array       $summary
 * @var Closure     $fmtDur
 */
?>

<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_ora1555_title') ?></h1>
</div>
<p style="font-size:.85rem;color:var(--color-text-muted);margin:.25rem 0 .75rem">
    <?= t('demo_oracle.diag_ora1555_hint') ?>
</p>

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

<!-- ══ KPI cards ════════════════════════════════════════════════════════ -->
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:.6rem;margin-bottom:.75rem">
    <div class="card" style="padding:.55rem .7rem">
        <h3 style="margin:0 0 .25rem;font-size:.85rem;color:var(--color-text-muted)">
            <?= t('demo_oracle.diag_ora1555_config') ?>
        </h3>
        <div style="font-size:.82rem;display:flex;flex-direction:column;gap:.15rem">
            <span><strong>undo_management</strong> : <code><?= htmlspecialchars($params['undo_management'] ?? '?') ?></code></span>
            <span><strong>undo_retention</strong> : <code><?= htmlspecialchars($params['undo_retention']  ?? '?') ?></code> s</span>
            <span><strong>undo_tablespace</strong> : <code><?= htmlspecialchars($params['undo_tablespace'] ?? '?') ?></code></span>
        </div>
    </div>

    <div class="card" style="padding:.55rem .7rem">
        <h3 style="margin:0 0 .25rem;font-size:.85rem;color:var(--color-text-muted)">
            <?= t('demo_oracle.diag_ora1555_errors') ?>
        </h3>
        <div style="font-size:1.4rem;font-weight:700;color:<?= $summary['ssolderr_total'] > 0 ? 'var(--color-danger)' : 'var(--color-success)' ?>;line-height:1">
            <?= $summary['ssolderr_total'] ?>
        </div>
        <small style="color:var(--color-text-muted)">
            ssolderrcnt — <?= t('demo_oracle.diag_ora1555_window_hint') ?>
        </small>
    </div>

    <div class="card" style="padding:.55rem .7rem">
        <h3 style="margin:0 0 .25rem;font-size:.85rem;color:var(--color-text-muted)">
            <?= t('demo_oracle.diag_ora1555_longest') ?>
        </h3>
        <div style="font-size:1.2rem;font-weight:700">
            <?= htmlspecialchars($fmtDur($summary['maxquerylen_max'])) ?>
        </div>
        <small style="color:var(--color-text-muted)">
            MAX(maxquerylen) — <?= t('demo_oracle.diag_ora1555_window_hint') ?>
        </small>
    </div>
</div>

<!-- ══ Tips ═════════════════════════════════════════════════════════════ -->
<div class="alert alert-info" style="margin-bottom:.75rem;font-size:.84rem;line-height:1.5">
    <strong><?= t('demo_oracle.diag_levers') ?></strong>
    <ul style="margin:.25rem 0 0 1.25rem">
        <li><?= t('demo_oracle.diag_ora1555_lever_retention') ?></li>
        <li><?= t('demo_oracle.diag_ora1555_lever_undo_size') ?></li>
        <li><?= t('demo_oracle.diag_ora1555_lever_long_queries') ?></li>
        <li><?= t('demo_oracle.diag_ora1555_lever_app') ?></li>
    </ul>
</div>

<!-- ══ Échantillons V$UNDOSTAT ══════════════════════════════════════════ -->
<?php if (!empty($rows)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem"><?= t('demo_oracle.diag_ora1555_samples') ?> (V$UNDOSTAT)</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">begin_time</th>
            <th style="text-align:left;padding:.3rem .5rem">end_time</th>
            <th style="text-align:right;padding:.3rem .5rem">undoblks</th>
            <th style="text-align:right;padding:.3rem .5rem">txncount</th>
            <th style="text-align:right;padding:.3rem .5rem">max_query_len</th>
            <th style="text-align:right;padding:.3rem .5rem">tuned_retention (s)</th>
            <th style="text-align:right;padding:.3rem .5rem">unxpstealcnt</th>
            <th style="text-align:right;padding:.3rem .5rem">expstealcnt</th>
            <th style="text-align:right;padding:.3rem .5rem">ssolderrcnt</th>
            <th style="text-align:right;padding:.3rem .5rem">nospaceerrcnt</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($rows as $r):
        $sso = (int)($r['SSOLDERRCNT'] ?? 0);
    ?>
        <tr style="border-bottom:1px solid var(--color-border);<?= $sso > 0 ? 'background:rgba(220,38,38,.06)' : '' ?>">
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem"><?= htmlspecialchars($r['BEGIN_TIME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem"><?= htmlspecialchars($r['END_TIME']   ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($r['UNDOBLKS']     ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($r['TXNCOUNT']     ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($r['MAXQUERYLEN']  ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($r['TUNED_UNDORETENTION'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($r['UNXPSTEALCNT'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($r['EXPSTEALCNT']  ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right<?= $sso > 0 ? ';color:var(--color-danger);font-weight:700' : '' ?>">
                <?= htmlspecialchars($r['SSOLDERRCNT'] ?? '') ?>
            </td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($r['NOSPACEERRCNT'] ?? '') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>
