<?php
require_once __DIR__.'/lib/v7_bootstrap.php';
$flash=['ok'=>'','err'=>''];
if($_SERVER['REQUEST_METHOD']==='POST'){
 itz_v7_csrf($csrf);$act=(string)($_POST['_action']??'');
 try{
  if($act==='toggle_module'){$id=(string)($_POST['module_id']??'');$enabled=isset($_POST['enabled'])?1:0;if(!isset($v7->modules()[$id]))throw new RuntimeException('ماژول معتبر نیست.');$s=$pdo->prepare('INSERT INTO itz_v7_module_state(module_id,enabled,updated_by,updated_at) VALUES(:i,:e,:u,:t) ON DUPLICATE KEY UPDATE enabled=VALUES(enabled),updated_by=VALUES(updated_by),updated_at=VALUES(updated_at)');$s->execute([':i'=>$id,':e'=>$enabled,':u'=>$_SESSION['user'],':t'=>time()]);itz_audit('module','toggle',$id,['enabled'=>$enabled]);$flash['ok']='وضعیت ماژول ذخیره شد.';}
  if($act==='save_flag'){$k=preg_replace('/[^a-z0-9_.-]/i','',(string)($_POST['flag_key']??''));if($k==='')throw new RuntimeException('کلید Feature Flag خالی است.');$e=isset($_POST['enabled'])?1:0;$p=max(0,min(100,(int)($_POST['rollout']??100)));$cfg=(string)($_POST['config_json']??'{}');$j=json_decode($cfg,true);if(!is_array($j))throw new RuntimeException('Config باید JSON معتبر باشد.');$s=$pdo->prepare('INSERT INTO itz_v7_feature_flags(flag_key,enabled,rollout_percent,config_json,updated_by,updated_at) VALUES(:k,:e,:p,:c,:u,:t) ON DUPLICATE KEY UPDATE enabled=VALUES(enabled),rollout_percent=VALUES(rollout_percent),config_json=VALUES(config_json),updated_by=VALUES(updated_by),updated_at=VALUES(updated_at)');$s->execute([':k'=>$k,':e'=>$e,':p'=>$p,':c'=>json_encode($j,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES),':u'=>$_SESSION['user'],':t'=>time()]);itz_audit('feature','save',$k,['enabled'=>$e,'rollout'=>$p]);$flash['ok']='Feature Flag ذخیره شد.';}
 }catch(Throwable $e){$flash['err']=$e->getMessage();}
}
$state=[];foreach($pdo->query('SELECT * FROM itz_v7_module_state')->fetchAll(PDO::FETCH_ASSOC)?:[] as $r)$state[$r['module_id']]=$r;
$flags=$pdo->query('SELECT * FROM itz_v7_feature_flags ORDER BY flag_key')->fetchAll(PDO::FETCH_ASSOC)?:[];
$stats=['events'=>(int)$pdo->query('SELECT COUNT(*) FROM itz_v7_events')->fetchColumn(),'automations'=>(int)$pdo->query('SELECT COUNT(*) FROM itz_v7_automations WHERE enabled=1')->fetchColumn(),'tasks'=>(int)$pdo->query("SELECT COUNT(*) FROM itz_v7_admin_tasks WHERE status_name='open'")->fetchColumn(),'flags'=>count($flags)];
?>
<!doctype html><html lang="fa" dir="rtl"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>iTz Architecture OS | iTzAYDiN-BOT</title><link rel="stylesheet" href="css/theme.css"><link rel="stylesheet" href="css/admin-extra.css"><link rel="stylesheet" href="css/ay-controls.css"><script src="js/theme.js" defer></script><style>
.v7hero{padding:24px;border:1px solid var(--border-soft);border-radius:24px;background:radial-gradient(circle at 85% 0,color-mix(in srgb,var(--accent) 20%,transparent),transparent 38%),linear-gradient(145deg,var(--surface-2),var(--surface-1));margin-bottom:16px}.v7k{font:800 10px monospace;letter-spacing:2px;color:var(--accent)}.statgrid,.modgrid,.flaggrid{display:grid;gap:12px}.statgrid{grid-template-columns:repeat(4,1fr);margin:15px 0}.modgrid{grid-template-columns:repeat(auto-fit,minmax(280px,1fr))}.flaggrid{grid-template-columns:repeat(auto-fit,minmax(320px,1fr))}.v7card{padding:15px;border:1px solid var(--border-soft);border-radius:18px;background:var(--surface-2)}.v7card small{color:var(--text-muted)}.caps{display:flex;flex-wrap:wrap;gap:5px;margin-top:10px}.caps span{font-size:10px;padding:4px 7px;border-radius:999px;background:color-mix(in srgb,var(--accent) 10%,transparent);color:var(--accent)}.v7stat b{font-size:25px}.v7actions{display:flex;gap:8px;flex-wrap:wrap;margin-top:12px}@media(max-width:700px){.statgrid{grid-template-columns:1fr 1fr}}</style></head><body><section id="container"><?php include 'header.php';?><section id="main-content"><div class="wrapper">
<div class="v7hero"><div class="v7k">iTz V7 • MODULAR CONTROL PLANE</div><h1>Architecture OS</h1><p>ماژول‌ها، Feature Flagها، Rollout تدریجی و Kill Switchها. قابلیت آزمایشی را بدون دست‌زدن به هسته روشن/خاموش کن.</p><div class="v7actions"><a class="btn btn-primary btn-sm" href="automations.php">Automation Studio</a><a class="btn btn-outline btn-sm" href="observability.php">Observability</a><a class="btn btn-outline btn-sm" href="customer360.php">Customer 360</a></div></div>
<?php if($flash['ok']):?><div class="alert alert-success"><?=itz_v7_h($flash['ok'])?></div><?php endif;?><?php if($flash['err']):?><div class="alert alert-danger"><?=itz_v7_h($flash['err'])?></div><?php endif;?>
<div class="statgrid"><?php foreach($stats as $k=>$n):?><div class="v7card v7stat"><small><?=itz_v7_h($k)?></small><b><?=number_format($n)?></b></div><?php endforeach;?></div>
<h2>Module Registry</h2><div class="modgrid"><?php foreach($v7->modules() as $id=>$m):$on=(int)($state[$id]['enabled']??!empty($m['default_enabled']));?><form method="post" class="v7card"><input type="hidden" name="_csrf" value="<?=itz_v7_h($csrf)?>"><input type="hidden" name="_action" value="toggle_module"><input type="hidden" name="module_id" value="<?=itz_v7_h($id)?>"><div style="display:flex;justify-content:space-between;gap:10px"><div><b><?=itz_v7_h($m['name'])?></b><small style="display:block">v<?=itz_v7_h($m['version'])?> · <?=itz_v7_h($m['description'])?></small></div><label><input type="checkbox" name="enabled" <?=$on?'checked':''?>> فعال</label></div><div class="caps"><?php foreach($m['capabilities'] as $c):?><span><?=itz_v7_h($c)?></span><?php endforeach;?></div><div style="margin-top:12px;text-align:left"><button class="btn btn-outline btn-sm">ذخیره</button></div></form><?php endforeach;?></div>
<h2 style="margin-top:22px">Feature Flags & Rollout</h2><div class="flaggrid"><?php foreach($flags as $f):?><form method="post" class="v7card"><input type="hidden" name="_csrf" value="<?=itz_v7_h($csrf)?>"><input type="hidden" name="_action" value="save_flag"><input type="hidden" name="flag_key" value="<?=itz_v7_h($f['flag_key'])?>"><div style="display:flex;justify-content:space-between"><code><?=itz_v7_h($f['flag_key'])?></code><label><input type="checkbox" name="enabled" <?=!empty($f['enabled'])?'checked':''?>> روشن</label></div><label>Rollout: <b><?=intval($f['rollout_percent'])?>%</b></label><input class="form-control" type="range" name="rollout" min="0" max="100" value="<?=intval($f['rollout_percent'])?>"><textarea class="form-control" name="config_json" rows="3" dir="ltr"><?=itz_v7_h($f['config_json']?:'{}')?></textarea><div style="text-align:left;margin-top:8px"><button class="btn btn-primary btn-sm">ذخیره</button></div></form><?php endforeach;?></div>
</div></section></section></body></html>
