<?php
/**
 * View — Diagnostic ORA-04031 « unable to allocate memory in shared pool ».
 *
 * @var string|null $fetchError
 * @var array       $sgaStats
 * @var float       $shFree
 * @var float       $shResFree
 * @var array       $libCache
 * @var array       $highVer
 * @var array       $params
 * @var Closure     $fmtBytes
 */
?>

<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_ora4031_title') ?></h1>
</div>
<p style="font-size:.85rem;color:var(--color-text-muted);margin:.25rem 0 .75rem">
    <?= t('demo_oracle.diag_ora4031_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(220px,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)">Shared pool — <?= t('demo_oracle.diag_ora4031_free') ?></h3>
        <div style="font-size:1.3rem;font-weight:700"><?= htmlspecialchars($fmtBytes($shFree)) ?></div>
        <small style="color:var(--color-text-muted)">
            <?= t('demo_oracle.diag_ora4031_reserved_free') ?> : <?= htmlspecialchars($fmtBytes($shResFree)) ?>
        </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_ora4031_config') ?></h3>
        <div style="font-size:.82rem;display:flex;flex-direction:column;gap:.15rem">
            <span><strong>sga_target</strong> : <?= htmlspecialchars($fmtBytes($params['sga_target'] ?? 0)) ?></span>
            <span><strong>shared_pool_size</strong> : <?= htmlspecialchars($fmtBytes($params['shared_pool_size'] ?? 0)) ?></span>
            <span><strong>shared_pool_reserved</strong> : <?= htmlspecialchars($fmtBytes($params['shared_pool_reserved_size'] ?? 0)) ?></span>
            <span><strong>cursor_sharing</strong> : <code><?= htmlspecialchars($params['cursor_sharing'] ?? '?') ?></code></span>
        </div>
    </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_ora4031_lever_bind') ?></li>
        <li><?= t('demo_oracle.diag_ora4031_lever_size') ?></li>
        <li><?= t('demo_oracle.diag_ora4031_lever_reserved') ?></li>
        <li><?= t('demo_oracle.diag_ora4031_lever_keep') ?></li>
    </ul>
</div>

<!-- Top consumers V$SGASTAT shared pool -->
<?php if (!empty($sgaStats)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_ora4031_top_alloc') ?> (V$SGASTAT, pool='shared pool')
</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">name</th>
            <th style="text-align:right;padding:.3rem .5rem">bytes</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($sgaStats as $s): ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($s['NAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right;font-weight:600"><?= htmlspecialchars($fmtBytes($s['BYTES'] ?? 0)) ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- Library cache -->
<?php if (!empty($libCache)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_ora4031_libcache') ?> (V$LIBRARYCACHE)
</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">namespace</th>
            <th style="text-align:right;padding:.3rem .5rem">gets</th>
            <th style="text-align:right;padding:.3rem .5rem">gethit%</th>
            <th style="text-align:right;padding:.3rem .5rem">pins</th>
            <th style="text-align:right;padding:.3rem .5rem">pinhit%</th>
            <th style="text-align:right;padding:.3rem .5rem">reloads</th>
            <th style="text-align:right;padding:.3rem .5rem">invalidations</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($libCache as $lc):
        $reloads = (int)($lc['RELOADS'] ?? 0);
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><strong><?= htmlspecialchars($lc['NAMESPACE'] ?? '') ?></strong></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($lc['GETS']        ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= number_format((float)($lc['GETHITRATIO'] ?? 0) * 100, 2, '.', '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($lc['PINS']        ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= number_format((float)($lc['PINHITRATIO'] ?? 0) * 100, 2, '.', '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right<?= $reloads > 100 ? ';color:var(--color-warning);font-weight:600' : '' ?>"><?= $reloads ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($lc['INVALIDATIONS'] ?? '') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- High VERSION_COUNT -->
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem">
    <?= t('demo_oracle.diag_ora4031_high_version') ?>
</h2>
<?php if (empty($highVer)): ?>
<div class="card"><p style="margin:0;color:var(--color-success)">✓ <?= t('demo_oracle.diag_ora4031_no_high_version') ?></p></div>
<?php else: ?>
<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">sql_id</th>
            <th style="text-align:right;padding:.3rem .5rem">version_count</th>
            <th style="text-align:left;padding:.3rem .5rem">parsing_schema</th>
            <th style="text-align:left;padding:.3rem .5rem">module</th>
            <th style="text-align:left;padding:.3rem .5rem">sql_preview</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($highVer as $h):
        $sqlId = (string)($h['SQL_ID'] ?? '');
        $vc    = (int)($h['VERSION_COUNT'] ?? 0);
    ?>
        <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;text-align:right;color:<?= $vc >= 100 ? 'var(--color-danger)' : 'var(--color-warning)' ?>;font-weight:700">
                <?= $vc ?>
            </td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($h['PARSING_SCHEMA_NAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-size:.76rem"><?= htmlspecialchars($h['MODULE']              ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.74rem"><?= htmlspecialchars($h['SQL_PREVIEW']         ?? '') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>
