<?php
/** AURA FARMING — Etapa 22: denúncia de jogador, transmissão ou comentário */
require_once __DIR__.'/../config/config.php';
require_once __DIR__.'/../config/database.php';
require_once __DIR__.'/../includes/functions.php';
requireLogin();
$uid=(int)$_SESSION['usuario_id'];
$targetId=(int)($_GET['usuario']??$_POST['reported_user_id']??0);
$partidaId=(int)($_GET['partida']??$_POST['partida_id']??0);
$competitionId=(int)($_GET['competicao']??$_POST['competition_id']??0);
$commentId=(int)($_GET['comentario']??$_POST['comment_id']??0);
$type=(string)($_GET['tipo']??$_POST['report_type']??($commentId?'comment':($partidaId?'transmission':'player')));
if($partidaId && !$targetId){$st=$pdo->prepare('SELECT usuario_id FROM af_partidas WHERE id=? LIMIT 1');$st->execute([$partidaId]);$targetId=(int)($st->fetchColumn()?:0);}
if($commentId && !$targetId){$st=$pdo->prepare('SELECT author_user_id,partida_id FROM af_live_comments WHERE id=? LIMIT 1');$st->execute([$commentId]);if($r=$st->fetch()){$targetId=(int)$r['author_user_id'];$partidaId=$partidaId?:((int)$r['partida_id']);}}
if($_SERVER['REQUEST_METHOD']==='POST'){
    try{
        if(!validarCsrfToken($_POST[CSRF_TOKEN_NAME]??''))throw new RuntimeException('Sessão expirada.');
        $result=afModerationCreateReport($pdo,$uid,[
            'reported_user_id'=>$targetId,'partida_id'=>$partidaId,'competition_id'=>$competitionId,'comment_id'=>$commentId,
            'report_type'=>$type,'category'=>(string)($_POST['category']??'other'),'description'=>(string)($_POST['description']??''),
            'block_user'=>!empty($_POST['block_user']),
        ]);
        setFlash('success','Denúncia enviada. Protocolo: '.$result['protocol']);
        header('Location: '.SITE_URL.'/pages/seguranca.php');exit;
    }catch(Throwable $e){setFlash('error',$e->getMessage());}
}
$target=null;if($targetId){$st=$pdo->prepare('SELECT id,nome,username,aura_id FROM af_usuarios WHERE id=? LIMIT 1');$st->execute([$targetId]);$target=$st->fetch();}
$labels=afModerationReportLabels();
$pageTitle='Enviar denúncia';$showNav=false;require __DIR__.'/../includes/header.php';
?>
<header class="app-header"><a href="<?=SITE_URL?>/pages/seguranca.php" class="btn-back">← Segurança</a><div class="logo-inline"><span class="logo-main">AURA</span><span class="logo-sub">SAFE</span></div><span class="aura-counter">ETAPA 22</span></header>
<main class="page-content moderation-user-page"><?=renderFlash()?>
<section class="card card--glow moderation-report-form">
  <p class="eyebrow">COMUNIDADE SEGURA</p><h1>Enviar denúncia</h1>
  <p class="text-muted">O relato será analisado com os registros técnicos disponíveis. Em situações graves envolvendo menores, o responsável é alertado.</p>
  <?php if($target):?><div class="security-approved-lock">Jogador: <?=esc($target['nome'])?> • <?=esc($target['aura_id'])?></div><?php endif;?>
  <form method="post" class="admin-form mt-16">
    <?=camposCsrf()?>
    <input type="hidden" name="reported_user_id" value="<?=$targetId?>"><input type="hidden" name="partida_id" value="<?=$partidaId?>"><input type="hidden" name="competition_id" value="<?=$competitionId?>"><input type="hidden" name="comment_id" value="<?=$commentId?>"><input type="hidden" name="report_type" value="<?=esc($type)?>">
    <label>Motivo<select name="category" required><?php foreach($labels as $key=>$label):?><option value="<?=esc($key)?>"><?=esc($label)?></option><?php endforeach;?></select></label>
    <label>O que aconteceu?<textarea name="description" rows="7" maxlength="3000" minlength="10" required placeholder="Descreva o ocorrido sem incluir dados pessoais desnecessários."></textarea></label>
    <?php if($targetId):?><label class="moderation-check"><input type="checkbox" name="block_user" value="1" checked><span><b>Bloquear este usuário agora</b><small>Ele não poderá seguir, assistir, comentar ou interagir com você.</small></span></label><?php endif;?>
    <button class="btn btn-primary">ENVIAR PARA ANÁLISE</button>
  </form>
</section></main>
<?php require __DIR__.'/../includes/footer.php';?>
