<?php
require_once __DIR__.'/../../_bootstrap.php';
$u=apiUsuario($pdo);$uid=(int)$u['id'];$age=afResumoIdade($pdo,$uid);
if(($age['tipo_conta']??'')==='minor'){
    $guardian=afGuardianResumoDoMenor($pdo,$uid);
    $consent=afConsentResumoDoMenor($pdo,$uid);
    jsonResponse(['ok'=>true,'role'=>'minor','guardian'=>$guardian,'consent'=>afConsentApiSummary($consent['consent']??null),'age'=>$age]);
}
if(afGuardianUsuarioPodeSerResponsavel($pdo,$uid)){
    $account=afGuardianGarantirConta($pdo,$uid,null,'api');
    $verification=afGuardianVerificationStatus($pdo,$uid);unset($verification['row']);
    $dependents=[];
    foreach(afGuardianDependentes($pdo,$uid) as $dep){
        $summary=afConsentResumoDoVinculo($pdo,(int)$dep['id'],$uid);
        $dep['consent']=afConsentApiSummary($summary['consent']??null);
        $controls=afGuardianDashboardEnsureControls($pdo,$uid,(int)$dep['id']);
        $access=afGuardianDashboardAccessDecision($pdo,(int)$dep['minor_usuario_id']);
        $dep['parental_controls']=afGuardianDashboardApiControls($controls,$access);
        $dep['activity_stats']=afGuardianDashboardStats($pdo,(int)$dep['minor_usuario_id']);
        $dependents[]=$dep;
    }
    jsonResponse(['ok'=>true,'role'=>'guardian','account'=>$account,'verification'=>$verification,'unread_alerts'=>count(afGuardianDashboardAlerts($pdo,$uid,100,true)),'dependents'=>$dependents,'age'=>$age]);
}
jsonResponse(['ok'=>false,'erro'=>'Conta sem classificação compatível com esta área.'],403);
