false,'error'=>'Aucun chemin detecte'];
$ok = false;
foreach (['/var/www/html/','/opt/weval-l99/','/opt/wevia-brain/'] as $px) { if (strpos($path,$px)===0) {$ok=true;break;} }
if (!$ok) return ['ok'=>false,'error'=>'Chemin non autorise'];
if (preg_match('/\.\./', $path)) return ['ok'=>false,'error'=>'Path traversal bloque'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$tpl_dir = __DIR__ . '/wevia-templates/';
$tpl_file = null;
if (preg_match('/presentation|slides|linkedin|big4|enterprise.*model|entreprise.*model/i', $msg)) {
$tf = $tpl_dir . 'presentation-4slides.html';
if (file_exists($tf)) $tpl_file = $tf;
}
if ($tpl_file && $ext === 'html') {
$tpl = file_get_contents($tpl_file);
$d = [];
$d['PAGE_TITLE'] = 'WEVIA Enterprise Model';
$d['S1_LOGO'] = 'W';
$d['S1_BRAND'] = 'WEVIA EM';
$d['S1_BRAND_SUB'] = 'ENTERPRISE MODEL';
$d['S1_COUNTER_VALUE'] = '669';
$d['S1_COUNTER_LABEL'] = 'Agents IA Autonomes';
$d['S1_TITLE'] = 'La workforce IA la plus complète.
17 départements. Un seul cerveau.';
$d['S1_TAGS'] = '
17 Depts
93 Crons
12 LLMs
0€ Lock-in
';
$d['S2_TITLE'] = 'Architecture SOA - 6 Couches';
$d['S2_SUBTITLE'] = 'Chaque couche pilotee par des agents specialises';
$d['S2_LAYERS'] = '🖥
Presentation
Dashboards - Portails - Chat
9 apps
🎯
Orchestration
WEVIA Master - BPMN - 17 Orchestrators
17 flux
🤖
Agents IA
Finance - RH - Marketing - Commerce - Supply - Prod
669
🧠
Intelligence
LLM Souverain 0EUR - RAG - KB 2490
12 LLMs
🔌
Data et Integration
API Gateway - ETL - 6.6M contacts
302 APIs
🛡
Gouvernance
Securite - Compliance - NonReg 152/152
100%
';
$d['S2_SIDEBAR'] = 'GOVERNANCE - SECURITY - COMPLIANCE';
$d['S3_BRAND'] = 'WEVIA EM';
$d['S3_SUBTITLE'] = 'VALUE STREAM MAPPING';
$d['S3_DEPTS'] = "{n:'Finance',c:'#4f7df9',y:.06},{n:'RH',c:'#22d3ee',y:.18},{n:'Marketing',c:'#f59e0b',y:.30},{n:'Commerce',c:'#10b981',y:.42},{n:'Supply Chain',c:'#ef4444',y:.54},{n:'Production',c:'#a78bfa',y:.66},{n:'SI & Data',c:'#4f7df9',y:.78},{n:'QA & Cyber',c:'#f472b6',y:.90}";
$d['S4_TITLE'] = 'Demarrez par un POC gratuit';
$d['S4_SUBTITLE'] = 'Choisissez un process metier. Nous deployons nos agents. Vous mesurez impact.';
$d['S4_CARDS'] = '🎯
POC Gratuit
1 process, resultats en 2 semaines
🚀
MVP - 1 Mois
3-5 process, ROI documente
🏢
Full Enterprise
17 depts, 669 agents, 3 mois
';
$d['S4_QUOTE'] = 'Les autres offrent une plateforme pour construire des agents.
WEVIA livre une workforce autonome complete - 669 agents prets.';
$d['S4_CTA_TEXT'] = 'Demander un POC gratuit';
$d['S4_CTA_URL'] = 'https://weval-consulting.com';
$d['S4_FOOTER'] = 'WEVAL Consulting - Casablanca - weval-consulting.com';
$content = $tpl;
foreach ($d as $key => $val) { $content = str_replace('{{' . $key . '}}', $val, $content); }
$content = preg_replace('/\{\{[A-Z0-9_]+\}\}/', '', $content);
if (file_exists($path)) @copy($path, $path . '.bak');
$w = @file_put_contents($path, $content);
if ($w === false) return ['ok'=>false,'error'=>'Ecriture echouee'];
$git = @shell_exec('cd /var/www/html && git add ' . escapeshellarg(basename($path)) . ' 2>&1 && git commit -m ' . escapeshellarg('wevia-master: template ' . basename($path)) . ' 2>&1|tail -3 && git push 2>&1|tail -2');
return ['ok'=>true,'path'=>$path,'size'=>strlen($content),'bytes'=>$w,'provider'=>'template+defaults','template'=>basename($tpl_file),'git'=>trim($git ?? ''),'message'=>"Fichier cree via template Opus: {$path} ({$w} bytes)"];
}
$sp = 'Tu es WEVIA Master. 669 agents, 17 departements, 93 crons, 12 LLMs. Dark theme. Code complet SANS backticks.';
$ch = curl_init('http://127.0.0.1:4000/v1/chat/completions');
curl_setopt_array($ch, [CURLOPT_POST=>true, CURLOPT_HTTPHEADER=>['Content-Type: application/json'], CURLOPT_POSTFIELDS=>json_encode(['messages'=>[['role'=>'system','content'=>$sp],['role'=>'user','content'=>"Cree {$path}: {$msg}"]],'max_tokens'=>16000,'stream'=>false]), CURLOPT_RETURNTRANSFER=>true, CURLOPT_TIMEOUT=>120]);
$resp = curl_exec($ch); $hc = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
if ($hc !== 200) return ['ok'=>false,'error'=>'LLM down'];
$data = @json_decode($resp, true);
$content = trim($data['choices'][0]['message']['content'] ?? '');
$content = preg_replace('/^```[a-z]*\n/', '', $content);
$content = preg_replace('/\n```[\s\S]*$/', '', $content);
if ($ext === 'html') { $he = strrpos($content, '