<?php
/** Resultado público compartilhável — Etapa 11 */
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';
$token=preg_replace('/[^a-f0-9]/','',strtolower((string)($_GET['t']??'')));
$p=afBuscarResultadoPublico($pdo,$token);
if($p && !afCanPublishUserContent($pdo,(int)$p['usuario_id']))$p=null;
if(!$p){http_response_code(404);$p=null;}else{afRegistrarVisitaViral($pdo,(int)$p['id'],null,'result');}
$score=$p?(float)$p['pontuacao_total']:0;
$title=$p?$p['nome'].' farmou '.number_format($score,2,',','.').' pontos de Aura':'Resultado não encontrado';
$description=$p?'Veja o resultado oficial de '.$p['aura_id'].' e tente superar essa pontuação.':'Este resultado não está disponível.';
$shareUrl=SITE_URL.'/pages/compartilhar.php?t='.urlencode($token);
$replayUrl=SITE_URL.'/pages/replay.php?t='.urlencode($token);
$wa='https://wa.me/?text='.rawurlencode('Eu encontrei uma farmada de '.$score.' pontos. Você consegue superar? '.$shareUrl);
$fb='https://www.facebook.com/sharer/sharer.php?u='.rawurlencode($shareUrl);
$x='https://twitter.com/intent/tweet?text='.rawurlencode('Você consegue superar '.$score.' pontos de Aura?').'&url='.rawurlencode($shareUrl);
?><!DOCTYPE html><html lang="pt-BR"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"><title><?=esc($title)?> | Aura Farming</title><meta name="description" content="<?=esc($description)?>"><meta property="og:type" content="website"><meta property="og:title" content="<?=esc($title)?>"><meta property="og:description" content="<?=esc($description)?>"><meta property="og:url" content="<?=esc($shareUrl)?>"><meta name="twitter:card" content="summary_large_image"><link rel="stylesheet" href="<?=SITE_URL?>/assets/css/style.css?v=11.0"></head><body class="share-result-body"><div class="particles"></div><main class="public-result-shell">
<?php if(!$p):?><section class="public-result-card"><div class="public-result-logo">AURA <small>FARMING</small></div><h1>Resultado não encontrado</h1><p>O link pode ter expirado ou sido digitado incorretamente.</p><a class="btn btn-primary" href="<?=SITE_URL?>">ABRIR AURA FARMING</a></section>
<?php else:$nivel=afNivelAuraVisual((float)$p['aura_gerada']);?>
<section class="public-result-card public-result-card--aura-<?=(int)$nivel['nivel']?>" data-public-share-url="<?=esc($shareUrl)?>">
 <div class="public-result-logo">AURA <small>FARMING</small></div>
 <div class="public-result-avatar"><img src="<?=urlAvatar($p['foto_perfil']??null,$p['foto_externa']??null)?>" alt=""><span><?=esc($p['aura_id'])?></span></div>
 <p class="eyebrow">EU FARMEI</p><h1><?=number_format($score,2,',','.')?></h1><h2>PONTOS DE AURA</h2>
 <?php if((int)($p['novo_recorde']??0)===1):?><div class="record-banner">🏅 NOVO RECORDE PESSOAL</div><?php endif;?>
 <div class="public-result-stats"><div><small>AURA</small><strong><?=number_format((float)$p['aura_gerada'],0,',','.')?></strong></div><div><small>LIKES</small><strong><?=(int)$p['likes_recebidos']?></strong></div><div><small>SUPER LIKES</small><strong><?=(int)$p['superlikes_recebidos']?></strong></div><div><small>RANKING GLOBAL</small><strong><?=!empty($p['ranking_global_posicao'])?(int)$p['ranking_global_posicao'].'º':'—'?></strong></div></div>
 <div class="public-result-tier"><?=esc($nivel['nome'])?></div><p class="public-result-challenge">Você consegue superar <?=esc($p['nome'])?>?</p>
 <canvas class="public-share-qr" width="180" height="180" data-public-qr="<?=esc($shareUrl)?>"></canvas>
 <a class="btn btn-primary" href="<?=SITE_URL?>">🔥 COMEÇAR A FARMAR</a><a class="btn btn-outline-cyan mt-12" href="<?=$replayUrl?>">▶ REPLAY ANIMADO</a><a class="btn btn-outline-cyan mt-12" href="<?=SITE_URL?>/pages/jogador.php?aura=<?=urlencode($p['aura_id'])?>">VER JOGADOR</a>
 <div class="public-network-row"><a href="<?=$wa?>" target="_blank" rel="noopener">💬</a><a href="<?=$fb?>" target="_blank" rel="noopener">f</a><a href="<?=$x?>" target="_blank" rel="noopener">𝕏</a><button type="button" data-public-copy>🔗</button></div><div class="share-feedback" data-public-feedback></div>
</section>
<script src="<?=SITE_URL?>/assets/vendor/qrcode/aura-qrcode.js?v=1.0"></script><script>(()=>{const c=document.querySelector('[data-public-qr]');try{AuraQRCode.render(c,c.dataset.publicQr,180)}catch(e){}const f=document.querySelector('[data-public-feedback]');document.querySelector('[data-public-copy]')?.addEventListener('click',async()=>{try{await navigator.clipboard.writeText(location.href);f.textContent='Link copiado!'}catch(e){prompt('Copie o link:',location.href)}})})();</script>
<?php endif;?></main></body></html>
