<?php
/** AURA FARMING — Etapa 11: central de compartilhamento e crescimento */
require_once __DIR__.'/../config/config.php';
require_once __DIR__.'/../config/database.php';
require_once __DIR__.'/../includes/functions.php';
require_once __DIR__.'/../includes/viral_functions.php';
requireLogin();
afRequireAgeFeature($pdo,'share');
$u=usuarioAtual();
$resumo=afResumoViral($pdo,(int)$u['id']);
$codigo=(string)($resumo['convite']['codigo']??'');
$inviteUrl=SITE_URL.'/auth/login.php?ref='.urlencode($codigo);
$pageTitle='Aura Viral';$activePage='';
$pageScripts=[SITE_URL.'/assets/vendor/qrcode/aura-qrcode.js?v=1.0',SITE_URL.'/assets/js/viral-dashboard.js?v=11.0'];
require_once __DIR__.'/../includes/header.php';
?>
<header class="app-header"><a href="<?=SITE_URL?>/pages/home.php" class="logo-inline"><span class="logo-main">AURA</span><span class="logo-sub">VIRAL</span></a><a href="<?=SITE_URL?>/pages/home.php" class="header-back">← Início</a></header>
<main class="page-content viral-center" data-invite-url="<?=esc($inviteUrl)?>">
<?=renderFlash()?>
<section class="viral-hero fade-in"><span class="viral-hero__orb">🚀</span><p class="eyebrow">ETAPA 11</p><h1>ESPALHE SUA AURA</h1><p>Crie cards, desafie amigos e acompanhe quantas pessoas chegaram pelos seus links.</p></section>

<section class="viral-metrics fade-in">
 <article><span>📤</span><strong><?=number_format((int)$resumo['compartilhamentos'],0,',','.')?></strong><small>COMPARTILHAMENTOS</small></article>
 <article><span>👀</span><strong><?=number_format((int)$resumo['visualizacoes'],0,',','.')?></strong><small>VISUALIZAÇÕES</small></article>
 <article><span>⚔️</span><strong><?=number_format((int)$resumo['desafios_criados'],0,',','.')?></strong><small>DESAFIOS CRIADOS</small></article>
 <article><span>🏆</span><strong><?=number_format((int)$resumo['desafios_superados'],0,',','.')?></strong><small>DESAFIOS SUPERADOS</small></article>
</section>

<section class="card viral-invite-card fade-in">
 <div><p class="eyebrow">SEU LINK PESSOAL</p><h2>CONVIDE NOVOS FARMERS</h2><p>O sistema registra cliques, contas criadas e a primeira farmada de cada convidado.</p></div>
 <canvas class="viral-qr" width="210" height="210" data-qr-value="<?=esc($inviteUrl)?>"></canvas>
 <div class="viral-code"><?=esc($codigo?:'INDISPONÍVEL')?></div>
 <div class="viral-invite-stats"><span><b><?=number_format((int)($resumo['convite']['cliques']??0),0,',','.')?></b> cliques</span><span><b><?=number_format((int)($resumo['convite']['cadastros']??0),0,',','.')?></b> cadastros</span><span><b><?=number_format((int)($resumo['convite']['primeiras_partidas']??0),0,',','.')?></b> ativos</span></div>
 <div class="viral-share-row"><button class="btn btn-primary" data-copy-invite>🔗 COPIAR LINK</button><button class="btn btn-outline-cyan" data-share-invite>📤 COMPARTILHAR</button></div>
 <div class="share-feedback" data-viral-feedback aria-live="polite"></div>
</section>

<div class="section-title">🔥 Seus resultados compartilháveis</div>
<section class="viral-results-list">
<?php if(!$resumo['resultados']):?><div class="card empty-inline">Conclua uma farmada para criar seu primeiro card.</div><?php else:foreach($resumo['resultados'] as $p):?>
 <article class="viral-result-row" data-result-id="<?=(int)$p['id']?>">
  <div><small><?=date('d/m/Y H:i',strtotime((string)$p['finalizada_em']))?></small><strong>💎 <?=number_format((float)$p['pontuacao_total'],2,',','.')?></strong><span>🌌 <?=number_format((float)$p['aura_gerada'],0,',','.')?> aura • <?=!empty($p['ranking_global_posicao'])?(int)$p['ranking_global_posicao'].'º global':'sem posição'?></span></div>
  <div class="viral-result-actions"><a href="<?=SITE_URL?>/pages/resultado.php?s=<?=urlencode((string)($p['sessao_id']??''))?>" class="btn-mini">ABRIR</a><button class="btn-mini btn-mini--hot" data-create-challenge>DESAFIAR</button><a href="<?=SITE_URL?>/pages/replay.php?t=<?=urlencode((string)$p['share_token'])?>" class="btn-mini">REPLAY</a></div>
 </article>
<?php endforeach;endif;?>
</section>

<div class="section-title">⚔️ Desafios recentes</div>
<section class="viral-challenge-list">
<?php if(!$resumo['desafios']):?><div class="card empty-inline">Nenhum desafio criado ou recebido.</div><?php else:foreach($resumo['desafios'] as $d):$euCriei=(int)$d['criador_id']===(int)$u['id'];?>
 <a class="viral-challenge-row" href="<?=SITE_URL?>/pages/desafio.php?t=<?=urlencode((string)$d['token'])?>"><span><?=match($d['status']){'concluido'=>'🏁','aceito'=>'⚡','aberto'=>'🔗',default=>'⏳'}?></span><div><strong><?=$euCriei?'Desafio enviado':'Desafio recebido'?> • <?=number_format((float)$d['alvo_pontuacao'],2,',','.')?></strong><small><?=esc(str_replace('_',' ',strtoupper((string)$d['status'])))?><?=!empty($d['convidado_nome'])?' • '.esc($d['convidado_nome']):''?></small></div><b>→</b></a>
<?php endforeach;endif;?>
</section>
</main>
<?php require_once __DIR__.'/../includes/footer.php';?>
