<?php
$policies = is_array($packagePolicies ?? null) ? $packagePolicies : [];
$decisions = is_array($policyDecisions ?? null) ? $policyDecisions : [];
$environment = (string)($policyEnvironment ?? 'local');
$generatedAt = (string)($policyGeneratedAt ?? '');
$h = static fn($v): string => htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
$activeCount = count(array_filter($policies, static fn(array $p): bool => (int)($p['active'] ?? 0) === 1));
$deniedCount = count(array_filter($decisions, static fn(array $d): bool => ($d['decision'] ?? '') === 'DENIED'));
?>
<style>
.axp{display:grid;gap:18px}.axp-hero{background:linear-gradient(135deg,#12233f,#244f76);color:#fff;border-radius:22px;padding:24px;display:flex;justify-content:space-between;align-items:center;gap:24px}.axp-card{background:#fff;border:1px solid #dbe4ef;border-radius:18px;padding:18px;box-shadow:0 10px 28px rgba(31,55,86,.06)}.axp-nav{display:flex;gap:8px;flex-wrap:wrap}.axp-btn{display:inline-flex;align-items:center;justify-content:center;padding:10px 14px;border-radius:11px;border:1px solid #cbd8e6;background:#fff;color:#183b5b;text-decoration:none;font-weight:750;cursor:pointer}.axp-btn.primary{background:#235f9d;color:#fff;border-color:#235f9d}.axp-btn.danger{color:#a51d2d;border-color:#e7bdc3}.axp-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:12px}.axp-kpi{font-size:30px;font-weight:900;color:#153552}.axp-pill{display:inline-flex;padding:6px 10px;border-radius:999px;background:#edf4fb;color:#1f5f96;font-weight:800}.axp-form{display:grid;grid-template-columns:repeat(12,1fr);gap:12px}.axp-field{grid-column:span 4}.axp-field.wide{grid-column:span 6}.axp-field.full{grid-column:1/-1}.axp-field label{display:block;font-weight:750;margin-bottom:6px}.axp-field input,.axp-field select,.axp-field textarea{width:100%;box-sizing:border-box;padding:10px 12px;border:1px solid #cbd8e6;border-radius:10px;background:#fff}.axp-checks{display:flex;gap:16px;flex-wrap:wrap;padding-top:8px}.axp-table-wrap{overflow:auto}.axp-table{width:100%;border-collapse:collapse;min-width:880px}.axp-table th,.axp-table td{padding:11px;border-bottom:1px solid #e5ebf2;text-align:left;vertical-align:middle}.axp-table th{font-size:12px;text-transform:uppercase;letter-spacing:.05em;color:#64748b}.axp-status{display:inline-flex;padding:5px 9px;border-radius:999px;font-size:12px;font-weight:850}.axp-status.ok{background:#dcfce7;color:#166534}.axp-status.warn{background:#fef3c7;color:#92400e}.axp-status.no{background:#fee2e2;color:#991b1b}.axp-policy{border-left:5px solid #87a9c8}.axp-policy.active{border-left-color:#2b7a4b}.axp-muted{color:#64748b}@media(max-width:820px){.axp-hero{flex-direction:column;align-items:flex-start}.axp-field,.axp-field.wide{grid-column:1/-1}}
</style>
<div class="axp">
  <nav class="axp-nav"><a class="axp-btn" href="/public/administration/plateforme/validation">Validation Center</a><a class="axp-btn" href="/public/administration/plateforme/confiance-packages">Trust Engine</a><a class="axp-btn" href="/public/administration/plateforme/compatibilite-packages">Compatibility Engine</a><a class="axp-btn" href="/public/administration/plateforme/certification-packages">Certification Center</a></nav>
  <section class="axp-hero"><div><small>PLATFORM-023.2.4 · AXIOM 10.1.4</small><h1>Package Policy Engine</h1><p>Décision finale d’installation selon l’environnement, la confiance, la compatibilité et la certification.</p></div><div><span class="axp-pill"><?= $h(strtoupper($environment)) ?></span><p>Environnement actif</p></div></section>
  <div class="axp-grid"><article class="axp-card"><small>Politiques actives</small><div class="axp-kpi"><?= $activeCount ?></div></article><article class="axp-card"><small>Décisions enregistrées</small><div class="axp-kpi"><?= count($decisions) ?></div></article><article class="axp-card"><small>Refus récents</small><div class="axp-kpi"><?= $deniedCount ?></div></article><article class="axp-card"><small>Dernière actualisation</small><strong><?= $h($generatedAt) ?></strong></article></div>
  <section class="axp-card"><h2>Évaluer un package importé</h2><form method="post" action="/public/administration/plateforme/politiques-packages/evaluer" class="axp-form"><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><div class="axp-field wide"><label for="package_id">Identifiant du package</label><input id="package_id" name="package_id" type="number" min="1" required><small class="axp-muted">Visible dans l’historique du Centre de mises à jour.</small></div><div class="axp-field"><label>&nbsp;</label><button class="axp-btn primary" type="submit">Évaluer la politique</button></div></form></section>
  <section class="axp-card"><h2>Créer ou modifier une politique</h2><form method="post" action="/public/administration/plateforme/politiques-packages/enregistrer" class="axp-form"><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><div class="axp-field wide"><label>Nom</label><input name="name" maxlength="150" required placeholder="Production Enterprise"></div><div class="axp-field"><label>Environnement</label><select name="environment_code"><option value="local">Local</option><option value="staging">Recette</option><option value="production">Production</option></select></div><div class="axp-field"><label>Priorité</label><input name="priority" type="number" min="0" max="999" value="100"></div><div class="axp-field"><label>Trust minimum</label><input name="min_trust_score" type="number" min="0" max="100" value="80"></div><div class="axp-field"><label>Compatibilité minimum</label><input name="min_compatibility_score" type="number" min="0" max="100" value="80"></div><div class="axp-field wide"><label>Éditeurs autorisés</label><input name="allowed_publishers" placeholder="AXIOM Enterprise, Éditeur interne"></div><div class="axp-field wide"><label>Autorités autorisées</label><input name="allowed_authorities" value="AXIOM_INTERNAL"></div><div class="axp-field full axp-checks"><label><input type="checkbox" name="active" checked> Active</label><label><input type="checkbox" name="require_certificate"> Certificat obligatoire</label><label><input type="checkbox" name="allow_warnings" checked> Autoriser les avertissements</label><label><input type="checkbox" name="block_development_packages"> Bloquer les packages de développement</label></div><div class="axp-field full"><button class="axp-btn primary" type="submit">Enregistrer la politique</button></div></form></section>
  <section class="axp-card"><h2>Politiques configurées</h2><div class="axp-grid"><?php foreach($policies as $p): ?><article class="axp-card axp-policy <?= (int)($p['active']??0)===1?'active':'' ?>"><div class="axp-nav"><span class="axp-status <?= (int)($p['active']??0)===1?'ok':'warn' ?>"><?= (int)($p['active']??0)===1?'ACTIVE':'INACTIVE' ?></span><span class="axp-pill"><?= $h(strtoupper($p['environment_code']??'')) ?></span></div><h3><?= $h($p['name']??'') ?></h3><p>Trust ≥ <strong><?= (int)($p['min_trust_score']??0) ?></strong> · Compatibilité ≥ <strong><?= (int)($p['min_compatibility_score']??0) ?></strong></p><p class="axp-muted">Certificat <?= (int)($p['require_certificate']??0)?'obligatoire':'facultatif' ?> · Priorité <?= (int)($p['priority']??0) ?></p><form method="post" action="/public/administration/plateforme/politiques-packages/statut"><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><input type="hidden" name="policy_id" value="<?= (int)($p['id']??0) ?>"><input type="hidden" name="active" value="<?= (int)($p['active']??0)===1?'0':'1' ?>"><button class="axp-btn" type="submit"><?= (int)($p['active']??0)===1?'Désactiver':'Activer' ?></button></form></article><?php endforeach; ?><?php if(!$policies): ?><p>Aucune politique configurée.</p><?php endif; ?></div></section>
  <section class="axp-card"><h2>Historique des décisions</h2><div class="axp-table-wrap"><table class="axp-table"><thead><tr><th>Date</th><th>Package</th><th>Version</th><th>Environnement</th><th>Score</th><th>Décision</th></tr></thead><tbody><?php foreach($decisions as $d): $class=($d['decision']??'')==='ALLOWED'?'ok':(($d['decision']??'')==='WARNING'?'warn':'no'); ?><tr><td><?= $h($d['created_at']??'') ?></td><td><?= $h($d['nom_package']??'') ?></td><td><?= $h($d['version_cible']??'') ?></td><td><?= $h($d['environment_code']??'') ?></td><td><strong><?= (int)($d['policy_score']??0) ?>/100</strong></td><td><span class="axp-status <?= $class ?>"><?= $h($d['decision']??'') ?></span></td></tr><?php endforeach; ?><?php if(!$decisions): ?><tr><td colspan="6">Aucune décision enregistrée.</td></tr><?php endif; ?></tbody></table></div></section>
</div>
