<?php
/**
 * View: Demo Oracle — TDE et portefeuilles.
 *
 * Portage de modules/oracle/views/report/dash_tde_wallets.php. La lecture est
 * identique ; le bouton d'activation de l'auto-login reste affiche mais
 * desactive, et la fenetre de saisie du mot de passe a disparu avec le
 * gestionnaire qui l'attendait.
 *
 * View — Dashboard TDE / Wallets.
 *
 * @var string|null $fetchError
 * @var array       $wallets    [{INST_ID, CON_ID, WRL_TYPE, WRL_PARAMETER, STATUS, WALLET_TYPE, WALLET_ORDER, FULLY_BACKED_UP}, …]
 * @var array       $tdeParams  [{NAME, VALUE}, …]
 * @var array       $encTs      [{NAME, ALGORITHM, STATUS}, …]
 * @var array       $keys       {TOTAL, LAST_ACTIVATION}
 * @var array       $oraDb
 */

/** Badge couleur selon le mode d'ouverture du wallet. */
$_tdeStatusClass = function (string $s): string {
    $s = strtoupper($s);
    if ($s === 'OPEN')                return 'badge-success';
    if ($s === 'CLOSED' || $s === 'NOT_AVAILABLE') return 'badge-danger';
    if ($s === 'OPEN_NO_MASTER_KEY')  return 'badge-warning';
    return 'badge-warning';
};
/** Auto-login ? d'après WALLET_TYPE. */
$_tdeIsAutologin = fn (string $t): bool => in_array(strtoupper($t), ['AUTOLOGIN', 'LOCAL_AUTOLOGIN'], true);

$primary = $wallets[0] ?? [];
?>

<div class="page-header"><h1 style="margin:0;font-size:1.05rem">🔐 <?= t('demo_oracle.dash_tde_title') ?></h1></div>
<p style="font-size:.85rem;color:var(--color-text-muted);margin:.25rem 0 .75rem">
    <?= t('demo_oracle.dash_tde_hint') ?>
</p>

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

<?php if (empty($wallets) && !$fetchError): ?>
<div class="card"><p style="margin:0;color:var(--color-text-muted)"><?= t('demo_oracle.dash_tde_no_wallet') ?></p></div>
<?php endif; ?>

<?php if (!empty($wallets)): ?>

<!-- KPIs (wallet principal) -->
<?php
$pStatus = (string)($primary['STATUS'] ?? '');
$pType   = (string)($primary['WALLET_TYPE'] ?? '');
$pWrl    = (string)($primary['WRL_TYPE'] ?? '');
$auto    = $_tdeIsAutologin($pType);
?>
<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)"><?= t('demo_oracle.dash_tde_status') ?></h3>
        <div><span class="badge <?= $_tdeStatusClass($pStatus) ?>" style="font-size:.78rem"><?= htmlspecialchars($pStatus !== '' ? $pStatus : '?') ?></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.dash_tde_autologin') ?></h3>
        <div style="font-size:1rem;font-weight:700;color:<?= $auto ? 'var(--color-success)' : 'var(--color-text)' ?>">
            <?= $auto ? t('demo_oracle.yes') : t('demo_oracle.no') ?>
        </div>
        <small style="color:var(--color-text-muted)"><code><?= htmlspecialchars($pType !== '' ? $pType : '?') ?></code></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.dash_tde_type') ?></h3>
        <div style="font-size:1rem;font-weight:700"><?= htmlspecialchars($pWrl !== '' ? $pWrl : '?') ?></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.dash_tde_enc_ts') ?></h3>
        <div style="font-size:1.4rem;font-weight:700;line-height:1"><?= count($encTs) ?></div>
    </div>
</div>

<!-- Wallets par instance -->
<div style="display:flex;align-items:center;justify-content:space-between;gap:.5rem;margin:.5rem 0 .35rem">
    <h2 style="font-size:.95rem;margin:0"><?= t('demo_oracle.dash_tde_wallets') ?> (GV$ENCRYPTION_WALLET)</h2>
    <?php if (!$auto): ?>
    <?php /* Le module complet ouvre ici une fenetre qui execute un
             ADMINISTER KEY MANAGEMENT pour creer le portefeuille auto-login.
             Le bouton reste affiche pour dire que la fonction existe. */ ?>
    <span style="display:inline-flex;align-items:center;gap:.5rem">
        <button type="button" class="btn btn-primary"
                style="font-size:.78rem;padding:.25rem .6rem;white-space:nowrap"<?= DemoOracleDemo::disabled() ?>>
            &#128275; <?= t('demo_oracle.dash_tde_make_autologin') ?>
        </button>
        <?= DemoOracleDemo::badge() ?>
    </span>
    <?php endif; ?>
</div>
<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_id</th>
            <th style="text-align:right;padding:.3rem .5rem">con_id</th>
            <th style="text-align:left;padding:.3rem .5rem"><?= t('demo_oracle.dash_tde_type') ?></th>
            <th style="text-align:left;padding:.3rem .5rem"><?= t('demo_oracle.dash_tde_location') ?></th>
            <th style="text-align:left;padding:.3rem .5rem"><?= t('demo_oracle.dash_tde_status') ?></th>
            <th style="text-align:left;padding:.3rem .5rem"><?= t('demo_oracle.dash_tde_autologin') ?></th>
            <th style="text-align:center;padding:.3rem .5rem"><?= t('demo_oracle.dash_tde_backed_up') ?></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($wallets as $w):
        $st   = (string)($w['STATUS'] ?? '');
        $wt   = (string)($w['WALLET_TYPE'] ?? '');
        $isAl = $_tdeIsAutologin($wt);
        $bk   = strtoupper((string)($w['FULLY_BACKED_UP'] ?? ''));
    ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($w['INST_ID'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;text-align:right"><?= htmlspecialchars($w['CON_ID'] ?? '') ?></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($w['WRL_TYPE'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem;word-break:break-all"><?= htmlspecialchars($w['WRL_PARAMETER'] ?? '') ?></td>
            <td style="padding:.3rem .5rem"><span class="badge <?= $_tdeStatusClass($st) ?>" style="font-size:.7rem"><?= htmlspecialchars($st) ?></span></td>
            <td style="padding:.3rem .5rem">
                <?php if ($isAl): ?>
                <span class="badge badge-success" style="font-size:.7rem"><?= htmlspecialchars($wt) ?></span>
                <?php else: ?>
                <span style="color:var(--color-text-muted)"><?= htmlspecialchars($wt !== '' ? $wt : '—') ?></span>
                <?php endif; ?>
            </td>
            <td style="padding:.3rem .5rem;text-align:center;font-size:.9rem">
                <?= $bk === 'YES' ? '<span style="color:var(--color-success)">✓</span>' : '<span style="color:var(--color-text-muted)">—</span>' ?>
            </td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>

<!-- Configuration TDE -->
<?php if (!empty($tdeParams)): ?>
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem"><?= t('demo_oracle.dash_tde_config') ?></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">
    <tbody>
    <?php foreach ($tdeParams as $p): ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem;font-weight:600;width:240px"><?= htmlspecialchars($p['NAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem;word-break:break-all"><?= htmlspecialchars(($p['VALUE'] ?? '') !== '' ? $p['VALUE'] : '—') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- Tablespaces chiffrés -->
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem"><?= t('demo_oracle.dash_tde_enc_ts') ?> (V$ENCRYPTED_TABLESPACES)</h2>
<?php if (empty($encTs)): ?>
<div class="card" style="margin-bottom:.75rem"><p style="margin:0;color:var(--color-text-muted)"><?= t('demo_oracle.dash_tde_no_enc_ts') ?></p></div>
<?php else: ?>
<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">tablespace</th>
            <th style="text-align:left;padding:.3rem .5rem"><?= t('demo_oracle.dash_tde_algorithm') ?></th>
            <th style="text-align:left;padding:.3rem .5rem"><?= t('demo_oracle.dash_tde_status') ?></th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($encTs as $e): ?>
        <tr style="border-bottom:1px solid var(--color-border)">
            <td style="padding:.3rem .5rem"><i class="fa-solid fa-lock" style="color:var(--color-text-muted);margin-right:.35rem"></i><?= htmlspecialchars($e['NAME'] ?? '') ?></td>
            <td style="padding:.3rem .5rem;font-family:monospace;font-size:.76rem"><?= htmlspecialchars($e['ALGORITHM'] ?? '') ?></td>
            <td style="padding:.3rem .5rem"><?= htmlspecialchars($e['STATUS'] ?? '') ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
</div>
<?php endif; ?>

<!-- Clés maîtres -->
<h2 style="font-size:.95rem;margin:.5rem 0 .35rem"><?= t('demo_oracle.dash_tde_keys') ?> (V$ENCRYPTION_KEYS)</h2>
<?php if (!empty($keys) && (int)($keys['TOTAL'] ?? 0) > 0): ?>
<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)"><?= t('demo_oracle.dash_tde_keys_count') ?></h3>
        <div style="font-size:1.4rem;font-weight:700;line-height:1"><?= (int)($keys['TOTAL'] ?? 0) ?></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.dash_tde_keys_last') ?></h3>
        <div style="font-family:monospace;font-size:.85rem"><?= htmlspecialchars(($keys['LAST_ACTIVATION'] ?? '') !== '' ? $keys['LAST_ACTIVATION'] : '—') ?></div>
    </div>
</div>
<?php else: ?>
<div class="card" style="margin-bottom:.75rem"><p style="margin:0;color:var(--color-text-muted)"><?= t('demo_oracle.dash_tde_keys_unavailable') ?></p></div>
<?php endif; ?>

<?php endif; ?>
