<?php
require_once __DIR__.'/../../_bootstrap.php';require_once ROOT_PATH.'/includes/match_functions.php';
if($_SERVER['REQUEST_METHOD']!=='POST')jsonResponse(['ok'=>false,'erro'=>'Método não permitido.'],405);afValidarCsrfApi();$u=afApiOuSessaoUsuario($pdo);$d=requestJson();$sessao=preg_replace('/[^a-f0-9]/','',strtolower((string)($d['sessao_id']??'')));
$st=$pdo->prepare('SELECT id,batalha_id FROM af_partidas WHERE usuario_id=? AND sessao_id=? LIMIT 1');$st->execute([(int)$u['id'],$sessao]);$p=$st->fetch();if(!$p||empty($p['batalha_id']))jsonResponse(['ok'=>false,'erro'=>'Partida de batalha inválida.'],422);
try{$b=afMarcarCameraProntaBatalha($pdo,(int)$p['batalha_id'],(int)$u['id'],(int)$p['id']);jsonResponse(['ok'=>true,'batalha'=>$b]);}catch(Throwable $e){jsonResponse(['ok'=>false,'erro'=>$e->getMessage()],422);}
