<?php
$reports = is_array($enterpriseReports ?? null) ? $enterpriseReports : [];
$selectedRow = is_array($selectedEnterpriseReport ?? null) ? $selectedEnterpriseReport : null;
$selected = is_array($selectedRow['report'] ?? null) ? $selectedRow['report'] : [];
$generatedAt = (string)($enterpriseReportGeneratedAt ?? '');
$availablePackages = is_array($enterpriseAvailablePackages ?? null) ? $enterpriseAvailablePackages : [];
$h = static fn($v): string => htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
$allowed = count(array_filter($reports, static fn(array $r): bool => ($r['decision'] ?? '') === 'INSTALLATION_AUTORISEE'));
$warning = count(array_filter($reports, static fn(array $r): bool => ($r['decision'] ?? '') === 'AUTORISEE_AVEC_AVERTISSEMENTS'));
$denied = count(array_filter($reports, static fn(array $r): bool => ($r['decision'] ?? '') === 'INSTALLATION_REFUSEE'));
$statusClass = static function(string $decision): string {
    return $decision === 'INSTALLATION_AUTORISEE' ? 'ok' : ($decision === 'AUTORISEE_AVEC_AVERTISSEMENTS' ? 'warn' : 'no');
};
?>
<style>
.axr{display:grid;gap:18px}.axr-nav{display:flex;gap:8px;flex-wrap:wrap}.axr-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}.axr-btn.primary{background:#235f9d;color:#fff;border-color:#235f9d}.axr-hero{background:linear-gradient(135deg,#12233f,#24577f);color:#fff;border-radius:22px;padding:24px;display:flex;justify-content:space-between;align-items:center;gap:24px}.axr-card{background:#fff;border:1px solid #dbe4ef;border-radius:18px;padding:18px;box-shadow:0 10px 28px rgba(31,55,86,.06)}.axr-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:12px}.axr-kpi{font-size:30px;font-weight:900;color:#153552}.axr-pill,.axr-status{display:inline-flex;padding:6px 10px;border-radius:999px;font-size:12px;font-weight:850}.axr-pill{background:#edf4fb;color:#1f5f96}.axr-status.ok{background:#dcfce7;color:#166534}.axr-status.warn{background:#fef3c7;color:#92400e}.axr-status.no{background:#fee2e2;color:#991b1b}.axr-form{display:grid;grid-template-columns:minmax(240px,720px) auto;gap:10px;align-items:end}.axr-form label{display:block;font-weight:750;margin-bottom:6px}.axr-form input{width:100%;box-sizing:border-box;padding:11px 12px;border:1px solid #cbd8e6;border-radius:10px}.axr-table-wrap{overflow:auto}.axr-table{width:100%;border-collapse:collapse;min-width:900px}.axr-table th,.axr-table td{padding:11px;border-bottom:1px solid #e5ebf2;text-align:left;vertical-align:middle}.axr-table th{font-size:12px;text-transform:uppercase;letter-spacing:.05em;color:#64748b}.axr-scores{display:grid;grid-template-columns:repeat(5,minmax(120px,1fr));gap:10px}.axr-score{padding:14px;border-radius:14px;background:#f4f7fb;border:1px solid #dbe4ef}.axr-score strong{display:block;font-size:25px;color:#153552}.axr-list{margin:0;padding-left:20px}.axr-list li{margin:5px 0}.axr-muted{color:#64748b}.axr-two{display:grid;grid-template-columns:1.2fr .8fr;gap:14px}.axr-meta{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px}.axr-meta div{padding:10px;background:#f8fafc;border-radius:10px}.axr-meta span{display:block;font-size:12px;color:#64748b}.axr-progress{height:10px;background:#e8eef5;border-radius:99px;overflow:hidden}.axr-progress i{display:block;height:100%;background:#235f9d}@media(max-width:900px){.axr-hero{flex-direction:column;align-items:flex-start}.axr-form,.axr-two{grid-template-columns:1fr}.axr-scores{grid-template-columns:repeat(2,1fr)}.axr-meta{grid-template-columns:1fr}}
</style>
<div class="axr">
  <nav class="axr-nav"><a class="axr-btn" href="/public/administration/plateforme/validation">Validation Center</a><a class="axr-btn" href="/public/administration/plateforme/confiance-packages">Trust Engine</a><a class="axr-btn" href="/public/administration/plateforme/compatibilite-packages">Compatibility Engine</a><a class="axr-btn" href="/public/administration/plateforme/certification-packages">Certification Center</a><a class="axr-btn" href="/public/administration/plateforme/politiques-packages">Package Policy</a></nav>
  <section class="axr-hero"><div><small>PLATFORM-023.2.5 · AXIOM 10.1.5</small><h1>Enterprise Compatibility Report</h1><p>Rapport consolidé de validation, confiance, compatibilité, certification et politique d’installation.</p></div><div><span class="axr-pill">DÉCISION ENTERPRISE</span><p>Dernière actualisation<br><strong><?= $h($generatedAt) ?></strong></p></div></section>
  <div class="axr-grid"><article class="axr-card"><small>Rapports</small><div class="axr-kpi"><?= count($reports) ?></div></article><article class="axr-card"><small>Autorisés</small><div class="axr-kpi"><?= $allowed ?></div></article><article class="axr-card"><small>Avec avertissements</small><div class="axr-kpi"><?= $warning ?></div></article><article class="axr-card"><small>Refusés</small><div class="axr-kpi"><?= $denied ?></div></article></div>
  <section class="axr-card"><h2>Analyser un package importé</h2><form method="post" action="/public/administration/plateforme/rapport-compatibilite/evaluer" class="axr-form"><div><label for="package_id">Package disponible</label><select id="package_id" name="package_id" required style="width:100%;box-sizing:border-box;padding:11px 12px;border:1px solid #cbd8e6;border-radius:10px;background:#fff"><option value="">Sélectionner un package importé</option><?php foreach($availablePackages as $pkg): ?><option value="<?= (int)($pkg['id']??0) ?>">#<?= (int)($pkg['id']??0) ?> — <?= $h($pkg['nom_package']??'Package') ?> — <?= $h($pkg['version_cible']??'version inconnue') ?> — <?= $h($pkg['statut']??'') ?></option><?php endforeach; ?></select><small class="axr-muted">Le numéro affiché après # est l’identifiant interne attendu par AXIOM.</small></div><button class="axr-btn primary" type="submit" <?= !$availablePackages ? 'disabled' : '' ?>>Générer le rapport</button><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"></form><?php if(!$availablePackages): ?><p class="axr-muted">Aucun package importé n’est disponible dans le Centre de mises à jour.</p><?php endif; ?></section>
  <?php if($selected): $scores=(array)($selected['scores']??[]); $manifest=(array)($selected['manifest']??[]); $decision=(string)($selected['decision']??'INSTALLATION_REFUSEE'); ?>
  <section class="axr-card"><div class="axr-nav" style="justify-content:space-between"><div><span class="axr-status <?= $statusClass($decision) ?>"><?= $h($decision) ?></span><h2><?= $h($manifest['package']??'Package') ?></h2><p class="axr-muted"><?= $h($manifest['publisher']??'') ?> · <?= $h($manifest['version']??'') ?> · <?= $h($manifest['package_revision']??'') ?></p></div><div class="axr-nav"><a class="axr-btn" target="_blank" href="/public/administration/plateforme/rapport-compatibilite/rapport?report_id=<?= (int)($selectedRow['id']??0) ?>">Rapport HTML / PDF</a><a class="axr-btn" href="/public/administration/plateforme/rapport-compatibilite/export.csv">Exporter CSV</a></div></div>
    <div class="axr-scores"><?php foreach(['global'=>'Global','trust'=>'Trust','compatibility'=>'Compatibilité','certification'=>'Certification','policy'=>'Politique'] as $key=>$label): $v=(int)($scores[$key]??0); ?><div class="axr-score"><span><?= $h($label) ?></span><strong><?= $v ?>/100</strong><div class="axr-progress"><i style="width:<?= $v ?>%"></i></div></div><?php endforeach; ?></div>
  </section>
  <div class="axr-two"><section class="axr-card"><h2>Identité et provenance</h2><div class="axr-meta"><div><span>UUID</span><strong><?= $h($manifest['package_uuid']??'—') ?></strong></div><div><span>Identifiant</span><strong><?= $h($manifest['package_id']??'—') ?></strong></div><div><span>Éditeur</span><strong><?= $h($manifest['publisher']??'—') ?></strong></div><div><span>Niveau de confiance</span><strong><?= $h($manifest['trust_level']??'—') ?></strong></div><div><span>Build</span><strong><?= $h($manifest['build_number']??'—') ?></strong></div><div><span>Signature</span><strong><?= $h($manifest['signature_algorithm']??'—') ?></strong></div></div></section>
  <section class="axr-card"><h2>Comparaison</h2><?php $cmp=$selected['comparison']??null; if(is_array($cmp)): ?><p>Rapport précédent : #<?= (int)$cmp['previous_report_id'] ?></p><p>Évolution du score : <strong><?= (int)$cmp['score_delta'] >= 0 ? '+' : '' ?><?= (int)$cmp['score_delta'] ?></strong></p><p>Décision précédente : <?= $h($cmp['previous_decision']) ?></p><p><?= !empty($cmp['decision_changed'])?'La décision a changé.':'La décision est stable.' ?></p><?php else: ?><p class="axr-muted">Première analyse de ce package.</p><?php endif; ?></section></div>
  <div class="axr-grid"><section class="axr-card"><h2>Blocages</h2><ul class="axr-list"><?php foreach((array)($selected['issues']??[]) as $x): ?><li><?= $h($x) ?></li><?php endforeach; ?><?php if(empty($selected['issues'])):?><li>Aucun blocage.</li><?php endif;?></ul></section><section class="axr-card"><h2>Avertissements</h2><ul class="axr-list"><?php foreach((array)($selected['warnings']??[]) as $x): ?><li><?= $h($x) ?></li><?php endforeach; ?><?php if(empty($selected['warnings'])):?><li>Aucun avertissement.</li><?php endif;?></ul></section><section class="axr-card"><h2>Risques détectés</h2><ul class="axr-list"><?php foreach((array)($selected['risks']??[]) as $x): ?><li><?= $h($x) ?></li><?php endforeach; ?><?php if(empty($selected['risks'])):?><li>Aucun risque complémentaire.</li><?php endif;?></ul></section></div>
  <?php endif; ?>
  <section class="axr-card"><div class="axr-nav" style="justify-content:space-between"><h2>Historique des décisions</h2><a class="axr-btn" href="/public/administration/plateforme/rapport-compatibilite/export.csv">Exporter CSV</a></div><div class="axr-table-wrap"><table class="axr-table"><thead><tr><th>Date</th><th>Package</th><th>Version</th><th>Global</th><th>Trust</th><th>Compatibilité</th><th>Certification</th><th>Politique</th><th>Décision</th><th></th></tr></thead><tbody><?php foreach($reports as $r): ?><tr><td><?= $h($r['created_at']??'') ?></td><td><?= $h($r['nom_package']??'') ?></td><td><?= $h($r['version_cible']??'') ?></td><td><strong><?= (int)($r['global_score']??0) ?></strong></td><td><?= (int)($r['trust_score']??0) ?></td><td><?= (int)($r['compatibility_score']??0) ?></td><td><?= (int)($r['certification_score']??0) ?></td><td><?= (int)($r['policy_score']??0) ?></td><td><span class="axr-status <?= $statusClass((string)($r['decision']??'')) ?>"><?= $h($r['decision']??'') ?></span></td><td><a class="axr-btn" href="?report_id=<?= (int)($r['id']??0) ?>">Détail</a></td></tr><?php endforeach; ?><?php if(!$reports): ?><tr><td colspan="10">Aucun rapport. Saisissez l’identifiant d’un package importé.</td></tr><?php endif; ?></tbody></table></div></section>
</div>
