<?php $d=$dashboard??[];$s=$d['settings']??[];$h=static fn($v):string=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8'); ?>
<style>
.ai{display:grid;gap:16px}.ai-card{background:#fff;border:1px solid #dce4ef;border-radius:18px;padding:18px}.ai-hero{background:linear-gradient(135deg,#172554,#4338ca);color:#fff}.ai-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(230px,1fr));gap:14px}.ai-btn{padding:10px 14px;border:0;border-radius:10px;background:#3730a3;color:#fff;font-weight:750;text-decoration:none;cursor:pointer}.ai-input,.ai-select{width:100%;box-sizing:border-box;padding:11px;border:1px solid #cbd5e1;border-radius:10px}.ai-answer{white-space:pre-wrap;background:#f8fafc;border-left:5px solid #4f46e5;padding:16px;border-radius:10px}.ai-table{width:100%;border-collapse:collapse;min-width:700px}.ai-table th,.ai-table td{padding:9px;border-bottom:1px solid #e5e7eb;text-align:left}.ai-scroll{overflow:auto}.ai-muted{color:#64748b}.ai-actions{display:flex;gap:8px;flex-wrap:wrap}.ai-badge{display:inline-block;padding:4px 8px;border-radius:999px;background:#e0e7ff;color:#312e81;font-weight:700}
</style>
<div class="ai">
<div class="ai-actions"><a class="ai-btn" href="/public/administration/securite/soc">← SOC</a><a class="ai-btn" href="/public/administration/securite/executive">Centre exécutif</a><a class="ai-btn" href="/public/administration/securite/audit">Audit</a></div>
<section class="ai-card ai-hero"><small>ENTERPRISE-008.11 · AXIOM 10.0.0</small><h1>AI Security Assistant</h1><p>Analyse assistée des incidents, détections, audits et politiques. Aucune action automatique : validation humaine obligatoire.</p><span class="ai-badge"><?= !empty($s['enabled'])?'ACTIF':'DÉSACTIVÉ' ?> · <?= $h($s['provider']??'local') ?></span></section>
<?php if(isset($_GET['saved'])):?><div class="ai-card">Configuration enregistrée.</div><?php endif?>
<section class="ai-card"><h2>Interroger l’assistant</h2><form method="post" action="/public/administration/securite/assistant-ia/interroger"><textarea class="ai-input" name="question" rows="5" maxlength="2000" required placeholder="Ex. Résume les risques prioritaires et propose un plan de traitement."><?= $h($question??'') ?></textarea><p class="ai-muted">Les adresses e-mail, téléphones, identifiants longs et secrets sont masqués avant traitement.</p><button class="ai-btn">Analyser</button></form></section>
<?php if($answer!==null):?><section class="ai-card"><h2>Réponse</h2><div class="ai-answer"><?= $h($answer['text']??'') ?></div><p class="ai-muted">Fournisseur : <?= $h($answer['provider']??'local') ?> · <?= (int)($answer['duration_ms']??0) ?> ms</p></section><?php endif?>
<div class="ai-grid"><section class="ai-card"><h3>Activité</h3><strong><?= (int)($d['stats']['total']??0) ?></strong> requête(s)<br><span><?= (int)($d['stats']['last24h']??0) ?> sur 24 h</span></section><section class="ai-card"><h3>Conformité</h3><strong><?= (int)($d['compliance']['score']??0) ?>/100</strong><br><span><?= $h($d['compliance']['status']??'non évalué') ?></span></section><section class="ai-card"><h3>Modèle</h3><strong><?= $h($s['model_name']??'axiom-security-local') ?></strong><br><span>Clé externe via AXIOM_AI_API_KEY uniquement</span></section></div>
<section class="ai-card"><h2>Configuration</h2><form method="post" action="/public/administration/securite/assistant-ia/configuration"><div class="ai-grid"><label>Fournisseur<select class="ai-select" name="provider"><?php foreach(['local'=>'Local AXIOM','openai'=>'OpenAI compatible','azure_openai'=>'Azure OpenAI','ollama'=>'Ollama'] as $k=>$v):?><option value="<?=$k?>" <?=($s['provider']??'local')===$k?'selected':''?>><?=$v?></option><?php endforeach?></select></label><label>Modèle<input class="ai-input" name="model" value="<?= $h($s['model_name']??'axiom-security-local') ?>"></label><label>Endpoint<input class="ai-input" name="endpoint" value="<?= $h($s['endpoint']??'') ?>" placeholder="http://localhost:11434/v1/chat/completions"></label></div><p><label><input type="checkbox" name="enabled" value="1" <?=!empty($s['enabled'])?'checked':''?>> Assistant actif</label> &nbsp; <label><input type="checkbox" name="allow_external" value="1" <?=!empty($s['allow_external'])?'checked':''?>> Autoriser les appels externes</label> &nbsp; <label><input type="checkbox" name="anonymize" value="1" <?=!empty($s['anonymize'])?'checked':''?>> Anonymisation obligatoire</label></p><button class="ai-btn">Enregistrer</button></form></section>
<section class="ai-card ai-scroll"><h2>Incidents récents</h2><table class="ai-table"><thead><tr><th>Titre</th><th>Gravité</th><th>Risque</th><th>Statut</th><th>Module</th></tr></thead><tbody><?php foreach(($d['incidents']??[]) as $i):?><tr><td><?= $h($i['title']) ?></td><td><?= $h($i['severity']) ?></td><td><?= (int)$i['risk_score'] ?>/100</td><td><?= $h($i['status']) ?></td><td><?= $h($i['module']) ?></td></tr><?php endforeach?><?php if(empty($d['incidents'])):?><tr><td colspan="5">Aucun incident.</td></tr><?php endif?></tbody></table></section>
<section class="ai-card ai-scroll"><h2>Historique anonymisé</h2><table class="ai-table"><thead><tr><th>Date</th><th>Fournisseur</th><th>Question</th><th>Statut</th></tr></thead><tbody><?php foreach(($d['history']??[]) as $x):?><tr><td><?= $h($x['created_at']) ?></td><td><?= $h($x['provider']) ?></td><td><?= $h($x['question_redacted']) ?></td><td><?= $h($x['status']) ?></td></tr><?php endforeach?><?php if(empty($d['history'])):?><tr><td colspan="4">Aucun historique.</td></tr><?php endif?></tbody></table><form method="post" action="/public/administration/securite/assistant-ia/historique/vider" onsubmit="return confirm('Vider tout l’historique IA ?')"><button class="ai-btn">Vider l’historique</button></form></section>
</div>
