<?php
/**
 * View — Diagnostic ORA-04030 « out of process memory » (PGA).
 *
 * @var string|null $fetchError
 * @var array       $params
 * @var array       $pgaStat
 * @var array       $topProc
 * @var array       $pgaCateg
 * @var Closure     $fmtBytes
 * @var Closure     $g
 * @var float       $overAlloc
 */
?>

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

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

<!-- KPIs -->
<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_ora4030_config') ?>
        </h3>
        <div style="font-size:.82rem;display:flex;flex-direction:column;gap:.15rem">
            <span><strong>pga_aggregate_target</strong> : <?= htmlspecialchars($fmtBytes($params['pga_aggregate_target'] ?? 0)) ?></span>
            <span><strong>pga_aggregate_limit</strong> : <?= htmlspecialchars($fmtBytes($params['pga_aggregate_limit'] ?? 0)) ?></span>
            <span><strong>workarea_size_policy</strong> : <code><?= htmlspecialchars($params['workarea_size_policy'] ?? '?') ?></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)">PGA in use / max</h3>
        <div style="font-size:1.2rem;font-weight:700"><?= htmlspecialchars($fmtBytes($g('total PGA inuse'))) ?></div>
        <small style="color:var(--color-text-muted)">
            max depuis startup : <?= htmlspecialchars($fmtBytes($g('maximum PGA allocated'))) ?>
        </small>
    </div>

    <div class="card" style="padding:.55rem .7rem">
        <h3 style="margin:0 0 .25rem;font-size:.85rem;color:var(--color-text-muted)">Cache hit %</h3>
        <?php $hit = $g('cache hit percentage'); ?>
        <div style="font-size:1.6rem;font-weight:700;color:<?= $hit >= 95 ? 'var(--color-success)' : ($hit >= 80 ? 'var(--color-warning)' : 'var(--color-danger)') ?>">
            <?= number_format($hit, 1, '.', '') ?> %
        </div>
        <small style="color:var(--color-text-muted)">cible : ≥ 95 %</small>
    </div>

    <div class="card" style="padding:.55rem .7rem">
        <h3 style="margin:0 0 .25rem;font-size:.85rem;color:var(--color-text-muted)">Over-allocations</h3>
        <div style="font-size:1.4rem;font-weight:700;color:<?= $overAlloc > 0 ? 'var(--color-danger)' : 'var(--color-success)' ?>">
            <?= (int)$overAlloc ?>
        </div>
        <small style="color:var(--color-text-muted)"><?= t('demo_oracle.diag_ora4030_overalloc_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_ora4030_lever_target') ?></li>
        <li><?= t('demo_oracle.diag_ora4030_lever_limit') ?></li>
        <li><?= t('demo_oracle.diag_ora4030_lever_topproc') ?></li>
        <li><?= t('demo_oracle.diag_ora4030_lever_sql') ?></li>
    </ul>
</div>

<!-- Top procs -->
<?php if (!empty($topProc)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_ora4030_top_proc') ?> (GV$PROCESS, top 25)
</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:right;padding:.3rem .5rem">inst</th>
            <th style="text-align:right;padding:.3rem .5rem">pid</th>
            <th style="text-align:left;padding:.3rem .5rem">spid</th>
            <th style="text-align:left;padding:.3rem .5rem">username</th>
            <th style="text-align:left;padding:.3rem .5rem">program</th>
            <th style="text-align:right;padding:.3rem .5rem">used</th>
            <th style="text-align:right;padding:.3rem .5rem">alloc</th>
            <th style="text-align:right;padding:.3rem .5rem">freeable</th>
            <th style="text-align:right;padding:.3rem .5rem">max</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($topProc as $p): ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($p['INST_ID'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($p['PID']     ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem"><?= htmlspecialchars($p['SPID'] ?? '') ?></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($p['USERNAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-size:.76rem"><?= htmlspecialchars($p['PROGRAM'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtBytes($p['PGA_USED_MEM']     ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right;font-weight:600"><?= htmlspecialchars($fmtBytes($p['PGA_ALLOC_MEM']    ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtBytes($p['PGA_FREEABLE_MEM'] ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtBytes($p['PGA_MAX_MEM']      ?? 0)) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- PGA breakdown by category -->
<?php if (!empty($pgaCateg)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_ora4030_by_category') ?> (GV$PROCESS_MEMORY)
</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">category</th>
            <th style="text-align:right;padding:.3rem .5rem">allocated</th>
            <th style="text-align:right;padding:.3rem .5rem">used</th>
            <th style="text-align:right;padding:.3rem .5rem">max_allocated</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($pgaCateg as $c): ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><strong><?= htmlspecialchars($c['CATEGORY'] ?? '') ?></strong></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtBytes($c['ALLOCATED']     ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtBytes($c['USED']          ?? 0)) ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($fmtBytes($c['MAX_ALLOCATED'] ?? 0)) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>
