<?php
require_once __DIR__.'/../../_bootstrap.php';require_once ROOT_PATH.'/includes/match_functions.php';$u=afApiOuSessaoUsuario($pdo);
if($_SERVER['REQUEST_METHOD']==='GET'){$token=preg_replace('/[^a-f0-9]/','',strtolower((string)($_GET['s']??'')));$p=afBuscarPartidaPublicaPorToken($pdo,$token,false,(int)$u['id']);if(!$p)jsonResponse(['ok'=>false,'erro'=>'Transmissão não encontrada.'],404);try{$items=afModerationComments($pdo,$p,(int)$u['id'],(int)($_GET['after']??0));jsonResponse(['ok'=>true,'comments'=>$items]);}catch(Throwable $e){jsonResponse(['ok'=>false,'erro'=>$e->getMessage()],403);}}
if($_SERVER['REQUEST_METHOD']!=='POST')jsonResponse(['ok'=>false,'erro'=>'Método não permitido.'],405);afValidarCsrfApi();$d=requestJson();$token=preg_replace('/[^a-f0-9]/','',strtolower((string)($d['s']??'')));$p=afBuscarPartidaPublicaPorToken($pdo,$token,false,(int)$u['id']);if(!$p)jsonResponse(['ok'=>false,'erro'=>'Transmissão não encontrada.'],404);
try{$comment=afModerationCreateComment($pdo,$p,(int)$u['id'],(string)($d['body']??''));jsonResponse(['ok'=>true,'comment'=>$comment]);}catch(Throwable $e){jsonResponse(['ok'=>false,'erro'=>$e->getMessage()],422);}
