<?php
require_once __DIR__.'/../config/config.php';
require_once __DIR__.'/../config/database.php';
require_once __DIR__.'/../includes/functions.php';
requireLogin();
$uid=(int)$_SESSION['usuario_id'];
if(!afGuardianUsuarioPodeSerResponsavel($pdo,$uid)){header('Location: '.SITE_URL.'/pages/home.php');exit;}
if($_SERVER['REQUEST_METHOD']==='POST'&&validarCsrfToken($_POST[CSRF_TOKEN_NAME]??'')){
    $id=(int)($_POST['alert_id']??0);$action=(string)($_POST['action']??'read');
    if($id>0)afGuardianDashboardMarkAlert($pdo,$uid,$id,$action==='resolve');
    header('Location: '.SITE_URL.'/pages/alertas-responsavel.php');exit;
}
$alerts=afGuardianDashboardAlerts($pdo,$uid,80,false);
$pageTitle='Alertas do responsável';$showNav=false;require_once __DIR__.'/../includes/header.php';
?>
<header class="app-header"><a href="<?=SITE_URL?>/pages/painel-responsavel.php" class="btn-back">← Painel</a><div class="logo-inline"><span class="logo-main">AURA</span><span class="logo-sub">ALERTAS</span></div><span class="btn-notif">🛡️</span></header>
<main class="page-content guardian-alert-page">
 <section class="guardian-dashboard-hero"><div class="guardian-shield">🔔</div><div><p class="eyebrow">ETAPA 20</p><h1>ALERTAS DO RESPONSÁVEL</h1><p>Sessões e incidentes técnicos da câmera segura, sem vídeo, imagem ou landmarks.</p></div></section>
 <?php if(!$alerts):?><div class="card guardian-empty"><span>✓</span><b>Nenhum alerta registrado</b><p>Quando houver atividade autorizada, os eventos aparecerão aqui.</p></div><?php else:?><section class="guardian-alert-list">
 <?php foreach($alerts as $a):?>
   <article class="card guardian-alert-row guardian-alert-row--<?=esc($a['severity'])?> <?=empty($a['read_at'])?'is-unread':''?>">
      <span><?=match($a['severity']){'critical'=>'!','warning'=>'⚠',default=>'●'}?></span>
      <div><small><?=esc($a['minor_nome'])?> • <?=esc($a['aura_id'])?></small><b><?=esc($a['title'])?></b><p><?=esc($a['message'])?></p><time><?=date('d/m/Y H:i',strtotime((string)$a['criado_em']))?></time></div>
      <form method="post"><?=camposCsrf()?><input type="hidden" name="alert_id" value="<?=(int)$a['id']?>"><button name="action" value="<?=empty($a['resolved_at'])?'resolve':'read'?>"><?=empty($a['resolved_at'])?'Resolver':'Lido'?></button></form>
   </article>
 <?php endforeach?></section><?php endif?>
</main>
<?php require_once __DIR__.'/../includes/footer.php';?>
