<?php
require_once __DIR__.'/../config/config.php';require_once __DIR__.'/../config/database.php';require_once __DIR__.'/../includes/functions.php';require_once __DIR__.'/../includes/match_functions.php';require_once __DIR__.'/../includes/admin_functions.php';afAdminRequireAuth();
$uid=afAdminId();
if($_SERVER['REQUEST_METHOD']==='POST'){
 try{if(!validarCsrfToken($_POST[CSRF_TOKEN_NAME]??''))throw new RuntimeException('Sessão expirada.');$partidaId=(int)($_POST['partida_id']??0);$decisao=(string)($_POST['decisao']??'');afRevisarPartida($pdo,$partidaId,$uid,$decisao,sanitize($_POST['observacao']??''));afAdminLog($pdo,'antifraude_'.$decisao,'partida',$partidaId);setFlash('success','Revisão registrada.');}
 catch(Throwable $e){setFlash('error',$e->getMessage());}header('Location: '.SITE_URL.'/pages/admin-seguranca.php');exit;
}
$filtro=(string)($_GET['status']??'revisao');if(!in_array($filtro,['revisao','rejeitada','aprovada','todos'],true))$filtro='revisao';$where=$filtro==='todos'?"p.status='finalizada'":"p.status='finalizada' AND p.antifraude_status=?";$params=$filtro==='todos'?[]:[$filtro];$st=$pdo->prepare("SELECT p.*,u.nome,u.username,u.aura_id,u.email,d.nome dispositivo_nome,d.risco dispositivo_risco,d.contas_vinculadas FROM af_partidas p JOIN af_usuarios u ON u.id=p.usuario_id LEFT JOIN af_dispositivos d ON d.id=p.dispositivo_id WHERE {$where} AND p.admin_status<>'anulada' ORDER BY p.antifraude_score DESC,p.finalizada_em DESC LIMIT 100");$st->execute($params);$items=$st->fetchAll()?:[];$totals=$pdo->query("SELECT SUM(antifraude_status='revisao') revisao,SUM(antifraude_status='rejeitada') rejeitada,SUM(antifraude_status='aprovada') aprovada FROM af_partidas WHERE status='finalizada' AND admin_status<>'anulada'")->fetch()?:[];
$pageTitle='Admin • Antifraude';$showNav=false;$bodyClass='admin-body';$adminPage='seguranca';require __DIR__.'/../includes/header.php';?>
<div class="admin-shell"><?php require __DIR__.'/../includes/admin_nav.php';?><main class="admin-main"><header class="admin-topbar"><div><small>SEGURANÇA</small><h1>Central antifraude</h1></div><span class="admin-count"><?=(int)($totals['revisao']??0)?> em revisão</span></header><?=renderFlash()?>
<section class="admin-kpi-grid report-kpis"><article><span>⌛</span><div><small>Revisão</small><strong><?=(int)($totals['revisao']??0)?></strong></div></article><article><span>✕</span><div><small>Rejeitadas</small><strong><?=(int)($totals['rejeitada']??0)?></strong></div></article><article><span>✓</span><div><small>Aprovadas</small><strong><?=(int)($totals['aprovada']??0)?></strong></div></article></section>
<nav class="admin-tabs"><?php foreach(['revisao'=>'Em revisão','rejeitada'=>'Rejeitadas','aprovada'=>'Aprovadas','todos'=>'Todas'] as $k=>$v):?><a class="<?=$filtro===$k?'active':''?>" href="?status=<?=$k?>"><?=$v?></a><?php endforeach;?></nav>
<div class="admin-cards-list"><?php if(!$items):?><div class="admin-empty">Nenhuma sessão neste filtro.</div><?php endif;foreach($items as $m):$reasons=json_decode((string)($m['antifraude_motivos']??'[]'),true)?:[];?><article class="admin-report-card"><header><div><span class="admin-pill status-<?=esc($m['antifraude_status'])?>"><?=esc($m['antifraude_status'])?></span><b>#<?=(int)$m['id']?> • <?=esc($m['nome'])?> • <?=esc($m['aura_id'])?></b></div><strong>Risco <?=(int)$m['antifraude_score']?></strong></header><div class="admin-report-grid"><p><small>PONTUAÇÃO</small><b><?=number_format((float)$m['pontuacao_total'],2,',','.')?></b><span>validada <?=number_format((float)$m['pontuacao_validada'],2,',','.')?></span></p><p><small>AMOSTRAS</small><b><?=(int)$m['total_amostras']?></b><span><?=(int)$m['amostras_duplicadas']?> duplicadas</span></p><p><small>LIVENESS</small><b><?=(int)$m['liveness_aprovados']?>/<?=(int)$m['liveness_desafios']?></b><span>Replay <?=number_format((float)$m['score_padrao_replay'],0,',','.')?>%</span></p></div><div class="security-reasons"><?php foreach($reasons as $r):?><span><?=esc(str_replace('_',' ',$r))?></span><?php endforeach;?></div><?php if(($m['antifraude_status']??'')!=='aprovada'):?><form method="post" class="admin-review-form"><?=camposCsrf()?><input type="hidden" name="partida_id" value="<?=(int)$m['id']?>"><input name="observacao" placeholder="Observação da revisão"><button class="admin-primary" name="decisao" value="aprovar">Aprovar</button><button class="admin-danger" name="decisao" value="rejeitar">Rejeitar</button><button name="decisao" value="monitorar">Monitorar</button><button name="decisao" value="suspender">Suspender 24h</button></form><?php endif;?></article><?php endforeach;?></div></main></div><script src="<?=SITE_URL?>/assets/js/admin.js?v=14.1"></script><?php require __DIR__.'/../includes/footer.php';?>
