<?php
use App\Core\Csrf;
$h = static fn($v): string => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');
$playbooks = is_array($playbooks ?? null) ? $playbooks : [];
$incidents = is_array($incidents ?? null) ? $incidents : [];
$runs = is_array($runs ?? null) ? $runs : [];
$run = is_array($run ?? null) ? $run : null;
?>
<style>.pb{display:grid;gap:16px}.pb-card{background:#fff;border:1px solid #dce4ef;border-radius:16px;padding:18px}.pb-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:12px}.pb-btn{display:inline-block;border:0;border-radius:9px;padding:9px 12px;background:#eaf1f9;color:#15385d;text-decoration:none;font-weight:750;cursor:pointer}.pb-btn.primary{background:#1d5f9f;color:#fff}.pb-btn.danger{background:#fee2e2;color:#991b1b}.pb-table{width:100%;border-collapse:collapse}.pb-table th,.pb-table td{padding:10px;border-bottom:1px solid #e8edf4;text-align:left}.pb-scroll{overflow:auto}.pb-pill{padding:4px 8px;border-radius:999px;background:#edf2f7;font-size:12px;font-weight:800}.pb-step{border-left:4px solid #aac2dc;padding:10px 12px;margin:8px 0;background:#f8fafc}.pb-actions{display:flex;gap:8px;flex-wrap:wrap}.pb label{display:grid;gap:5px;margin:8px 0}.pb select,.pb textarea{padding:9px;border:1px solid #cfd9e6;border-radius:8px;width:100%;box-sizing:border-box}</style>
<div class="pb"><div class="pb-actions"><a class="pb-btn" href="/public/administration/securite/soc">← SOC</a></div><section class="pb-card"><small>ENTERPRISE-008.5.1 · AXIOM 9.9.1.1</small><h1>SOC Automation & Playbook Engine</h1><p>Réponses guidées, réversibles et soumises à validation humaine. Aucun blocage de compte ni action destructive n’est exécuté automatiquement.</p></section>
<section class="pb-card"><h2>Démarrer un playbook</h2><form method="post" action="/public/administration/securite/soc/playbooks/demarrer"><?= Csrf::field() ?><div class="pb-grid"><label>Playbook<select name="playbook_id" required><option value="">Sélectionner</option><?php foreach($playbooks as $p):?><option value="<?= (int)$p['id'] ?>"><?= $h($p['name']) ?> · <?= (int)$p['step_count'] ?> étapes</option><?php endforeach?></select></label><label>Incident actif<select name="incident_id" required><option value="">Sélectionner</option><?php foreach($incidents as $i):?><option value="<?= (int)$i['id'] ?>"><?= $h($i['title']) ?> · <?= (int)$i['risk_score'] ?>/100</option><?php endforeach?></select></label></div><button class="pb-btn primary">Démarrer avec validation humaine</button></form></section>
<section class="pb-grid"><?php foreach($playbooks as $p):?><article class="pb-card"><span class="pb-pill"><?= $h($p['code']) ?></span><h3><?= $h($p['name']) ?></h3><p><?= $h($p['description']) ?></p><small>Seuil conseillé : <?= (int)$p['trigger_score_min'] ?>/100 · <?= (int)$p['step_count'] ?> étapes</small></article><?php endforeach?></section>
<section class="pb-card pb-scroll"><h2>Exécutions récentes</h2><table class="pb-table"><thead><tr><th>Playbook</th><th>Incident</th><th>Statut</th><th>Étape</th><th>Mise à jour</th><th></th></tr></thead><tbody><?php foreach($runs as $r):?><tr><td><?= $h($r['playbook_name']) ?></td><td><?= $h($r['incident_title']) ?><br><small><?= $h($r['incident_uuid']) ?></small></td><td><span class="pb-pill"><?= $h($r['status']) ?></span></td><td><?= (int)$r['current_step'] ?></td><td><?= $h($r['updated_at']) ?></td><td><a class="pb-btn" href="?run=<?= (int)$r['id'] ?>">Ouvrir</a></td></tr><?php endforeach?><?php if(!$runs):?><tr><td colspan="6">Aucune exécution.</td></tr><?php endif?></tbody></table></section>
<?php if($run):?><section class="pb-card"><h2><?= $h($run['playbook_name']) ?></h2><p><strong>Incident :</strong> <?= $h($run['incident_title']) ?> · risque <?= (int)$run['risk_score'] ?>/100</p><div class="pb-actions"><form method="post" action="/public/administration/securite/soc/playbooks/statut"><?= Csrf::field() ?><input type="hidden" name="run_id" value="<?= (int)$run['id'] ?>"><button class="pb-btn" name="status" value="paused">Pause</button><button class="pb-btn" name="status" value="running">Reprendre</button><button class="pb-btn danger" name="status" value="cancelled">Annuler</button></form></div><?php foreach($run['steps'] as $s):?><div class="pb-step"><strong>Étape <?= (int)$s['step_order'] ?> · <?= $h($s['name']) ?></strong> <span class="pb-pill"><?= $h($s['status']) ?></span><p><?= $h($s['description']) ?></p><?php if($s['output_text']):?><small><?= nl2br($h($s['output_text'])) ?></small><?php endif?><?php if($s['status']==='waiting_approval' && $run['status']!=='cancelled'):?><form method="post" action="/public/administration/securite/soc/playbooks/etape"><?= Csrf::field() ?><input type="hidden" name="run_id" value="<?= (int)$run['id'] ?>"><label>Note de validation<textarea name="note" rows="3"></textarea></label><button class="pb-btn primary" name="decision" value="approve">Valider et exécuter</button> <button class="pb-btn" name="decision" value="skip">Ignorer l’étape</button></form><?php endif?></div><?php endforeach?></section><?php endif?></div>
