<?php
$h=fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');
$old=$_SESSION['automation_old']??[];unset($_SESSION['automation_old']);$form=$old?:($edit?:[]);
$triggers=['step.created'=>'Étape créée','step.updated'=>'Étape modifiée','step.completed'=>'Étape terminée','milestone.completed'=>'Jalon atteint','release.completed'=>'Release terminée'];
$actions=['audit_log'=>'Écrire dans le journal','set_release_status'=>'Changer le statut de la release','set_release_progress'=>'Valider une progression cible'];
?>
<style>
.auto{display:grid;gap:16px}.auto-card{background:#fff;border:1px solid #dbe5ef;border-radius:16px;padding:18px}.auto-hero{display:flex;justify-content:space-between;gap:16px;align-items:center}.auto h1,.auto h2{margin:0;color:#173b5a}.auto-grid{display:grid;grid-template-columns:minmax(300px,.75fr) minmax(0,1.25fr);gap:16px}.auto-form{display:grid;gap:10px}.auto-form label{font-size:12px;font-weight:800;color:#516579}.auto-form input,.auto-form select,.auto-form textarea{width:100%;box-sizing:border-box;padding:10px;border:1px solid #cbd8e5;border-radius:9px}.auto-btn{display:inline-flex;padding:9px 12px;border:0;border-radius:9px;background:#e8f0f7;color:#17466b;font-weight:800;text-decoration:none;cursor:pointer}.auto-btn.primary{background:#1769aa;color:#fff}.auto-btn.danger{background:#fee2e2;color:#991b1b}.auto-actions{display:flex;gap:7px;flex-wrap:wrap}.auto-table-wrap{overflow:auto}.auto-table{width:100%;border-collapse:collapse;min-width:860px}.auto-table th,.auto-table td{padding:10px;border-bottom:1px solid #e5edf4;text-align:left;vertical-align:top}.auto-badge{display:inline-flex;padding:5px 8px;border-radius:999px;background:#eef4f8;font-size:12px;font-weight:800}.auto-badge.on{background:#dcfce7;color:#166534}.auto-badge.off{background:#f1f5f9;color:#475569}.auto-muted{color:#66788a;font-size:13px}@media(max-width:900px){.auto-grid{grid-template-columns:1fr}.auto-hero{align-items:flex-start;flex-direction:column}}
</style>
<div class="auto">
<section class="auto-card auto-hero"><div><small>PLATFORM-023.5.6 · Livraison 2.1.1</small><h1>Release Automation</h1><p>Socle sécurisé des règles, simulations et journaux d’exécution.</p></div><div class="auto-actions"><a class="auto-btn" href="/public/administration/plateforme/release-trains">Release Trains</a><a class="auto-btn" href="/public/administration">Retour</a></div></section>
<div class="auto-grid">
<section class="auto-card"><h2><?= $edit?'Modifier la règle':'Nouvelle règle' ?></h2><form class="auto-form" method="post" action="/public/administration/plateforme/release-automation/enregistrer">
<input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><input type="hidden" name="id" value="<?= (int)($form['id']??0) ?>">
<label>Release Train<select name="release_train_id" required><option value="">Sélectionner</option><?php foreach($trains as $train): ?><option value="<?= (int)$train['id'] ?>" <?= (int)($form['release_train_id']??0)===(int)$train['id']?'selected':'' ?>><?= $h($train['code'].' — '.$train['name']) ?></option><?php endforeach ?></select></label>
<label>Nom<input name="name" maxlength="180" required value="<?= $h($form['name']??'') ?>"></label>
<label>Description<textarea name="description" rows="3"><?= $h($form['description']??'') ?></textarea></label>
<label>Déclencheur<select name="trigger_event" required><?php foreach($triggers as $key=>$label): ?><option value="<?= $h($key) ?>" <?= ($form['trigger_event']??'')===$key?'selected':'' ?>><?= $h($label) ?></option><?php endforeach ?></select></label>
<label>Action<select name="action_type" required><?php foreach($actions as $key=>$label): ?><option value="<?= $h($key) ?>" <?= ($form['action_type']??'')===$key?'selected':'' ?>><?= $h($label) ?></option><?php endforeach ?></select></label>
<label>Paramètres JSON<textarea name="parameters_json" rows="4" spellcheck="false"><?= $h($form['parameters_json']??'{}') ?></textarea><span class="auto-muted">Exemples : {"status":"active"} ou {"progress":50}</span></label>
<label>Priorité<input type="number" name="priority" min="1" max="9999" value="<?= (int)($form['priority']??100) ?>"></label>
<div class="auto-actions"><button class="auto-btn primary" type="submit">Enregistrer</button><?php if($edit): ?><a class="auto-btn" href="/public/administration/plateforme/release-automation">Annuler</a><?php endif ?></div>
</form></section>
<section class="auto-card"><h2>Règles</h2><div class="auto-table-wrap"><table class="auto-table"><thead><tr><th>Priorité</th><th>Release</th><th>Règle</th><th>Déclencheur</th><th>Action</th><th>Statut</th><th>Actions</th></tr></thead><tbody>
<?php foreach($rules as $rule): ?><tr><td><?= (int)$rule['priority'] ?></td><td><?= $h($rule['release_code']) ?></td><td><strong><?= $h($rule['name']) ?></strong><br><span class="auto-muted"><?= $h($rule['description']) ?></span></td><td><?= $h($triggers[$rule['trigger_event']]??$rule['trigger_event']) ?></td><td><?= $h($actions[$rule['action_type']]??$rule['action_type']) ?></td><td><span class="auto-badge <?= (int)$rule['is_active']===1?'on':'off' ?>"><?= (int)$rule['is_active']===1?'Active':'Inactive' ?></span></td><td><div class="auto-actions"><a class="auto-btn" href="?edit=<?= (int)$rule['id'] ?>">Modifier</a>
<form method="post" action="/public/administration/plateforme/release-automation/simuler"><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><input type="hidden" name="id" value="<?= (int)$rule['id'] ?>"><button class="auto-btn" type="submit">Simuler</button></form>
<form method="post" action="/public/administration/plateforme/release-automation/statut"><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><input type="hidden" name="id" value="<?= (int)$rule['id'] ?>"><input type="hidden" name="active" value="<?= (int)$rule['is_active']===1?0:1 ?>"><button class="auto-btn" type="submit"><?= (int)$rule['is_active']===1?'Désactiver':'Activer' ?></button></form>
<form method="post" action="/public/administration/plateforme/release-automation/archiver" onsubmit="return confirm('Archiver cette règle ?')"><input type="hidden" name="_csrf" value="<?= $h(\App\Core\Csrf::token()) ?>"><input type="hidden" name="id" value="<?= (int)$rule['id'] ?>"><button class="auto-btn danger" type="submit">Archiver</button></form></div></td></tr><?php endforeach ?>
<?php if(!$rules): ?><tr><td colspan="7">Aucune règle créée.</td></tr><?php endif ?></tbody></table></div></section></div>
<section class="auto-card"><h2>Journal d’exécution</h2><div class="auto-table-wrap"><table class="auto-table"><thead><tr><th>Date</th><th>Release</th><th>Règle</th><th>Mode</th><th>Résultat</th><th>Durée</th><th>Message</th></tr></thead><tbody><?php foreach($logs as $log): ?><tr><td><?= $h($log['created_at']) ?></td><td><?= $h($log['release_code']) ?></td><td><?= $h($log['rule_name']) ?></td><td><?= (int)$log['simulation']===1?'Simulation':'Réel' ?></td><td><span class="auto-badge <?= $log['status']==='success'?'on':'off' ?>"><?= $h($log['status']) ?></span></td><td><?= (int)$log['duration_ms'] ?> ms</td><td><?= $h($log['message']) ?></td></tr><?php endforeach ?><?php if(!$logs): ?><tr><td colspan="7">Aucune exécution enregistrée.</td></tr><?php endif ?></tbody></table></div></section>
</div>
