<?php
require_once __DIR__.'/../../_bootstrap.php';
$u=apiUsuario($pdo);$uid=(int)$u['id'];
if($_SERVER['REQUEST_METHOD']!=='POST')jsonResponse(['ok'=>false,'erro'=>'Método não permitido.'],405);
$age=afResumoIdade($pdo,$uid);
if(($age['tipo_conta']??'')!=='minor')jsonResponse(['ok'=>false,'erro'=>'Somente a conta do menor pode gerar o código familiar.'],403);
try{
    $generated=afGuardianGenerateFamilyCode($pdo,$uid);
    jsonResponse(['ok'=>true,'family_code'=>[
        'code'=>$generated['code'],
        'expires_minutes'=>(int)$generated['expires_minutes'],
        'expires_at'=>$generated['expires_at'],
        'guardian_name'=>$generated['guardian_name'],
    ]],201);
}catch(Throwable $e){jsonResponse(['ok'=>false,'erro'=>$e->getMessage()],422);}
