<?php
use App\Core\Permission;

$firstName = trim((string)($user['prenom'] ?? '')) ?: 'CDA';
$canClients = Permission::can('clients.view');
$canProfessors = Permission::can('professeurs.view');
$canWorkshops = Permission::can('ateliers.view');
$canGroups = Permission::can('groupes.view');
?>
<section class="pilot-hero">
    <div>
        <span class="pilot-eyebrow">Centre de pilotage</span>
        <h2>Bonjour <?= htmlspecialchars($firstName) ?> 👋</h2>
        <p>Les indicateurs et raccourcis sont affichés selon vos permissions.</p>
    </div>
    <div class="pilot-actions">
        <?php if (Permission::can('clients.create')): ?><a class="pilot-btn primary" href="/public/clients/nouveau">＋ Nouvel adhérent</a><?php endif; ?>
        <?php if (Permission::can('ateliers.create')): ?><a class="pilot-btn" href="/public/ateliers/nouveau">＋ Nouvel atelier</a><?php endif; ?>
        <?php if (Permission::canAny('updates.view','backups.view','administration.permissions.view','utilisateurs.view')): ?><a class="pilot-btn" href="/public/administration/axiom">◇ AXIOM Studio</a><?php endif; ?>
    </div>
</section>

<section class="pilot-kpis">
    <?php if ($canClients): ?><a href="/public/clients" class="pilot-kpi"><span>👥</span><small>Adhérents</small><strong><?= (int)$stats['clients'] ?></strong><em><?= (int)$stats['familles'] ?> famille(s)</em></a><?php endif; ?>
    <?php if ($canProfessors): ?><a href="/public/professeurs" class="pilot-kpi"><span>🎼</span><small>Professeurs</small><strong><?= (int)$stats['professeurs'] ?></strong><em>Intervenants actifs</em></a><?php endif; ?>
    <?php if ($canWorkshops): ?><a href="/public/ateliers" class="pilot-kpi"><span>🎨</span><small>Ateliers</small><strong><?= (int)$stats['ateliers'] ?></strong><em><?= (int)$capacityTotals['full'] ?> complet(s)</em></a><?php endif; ?>
    <?php if ($canGroups): ?><a href="/public/groupes" class="pilot-kpi"><span>📅</span><small>Inscriptions</small><strong><?= (int)$stats['inscriptions'] ?></strong><em><?= (int)$stats['groupes'] ?> groupe(s)</em></a><?php endif; ?>
    <?php if ($canWorkshops): ?><a href="/public/ateliers" class="pilot-kpi"><span>◎</span><small>Remplissage</small><strong><?= (int)$capacityTotals['rate'] ?>%</strong><em><?= (int)$capacityTotals['free'] ?> place(s) libre(s)</em></a><?php endif; ?>
</section>

<section class="pilot-grid">
    <?php if (!empty($directionTasks)): ?><article class="pilot-panel pilot-wide">
        <header><div><span class="pilot-eyebrow">Direction</span><h3>Tâches à traiter <strong><?= count($directionTasks) ?></strong></h3></div></header>
        <div class="pilot-people"><?php foreach ($directionTasks as $task): ?><a href="<?= htmlspecialchars((string)$task['url']) ?>"><span><?= $task['kind']==='teletravail'?'⌂':($task['kind']==='planning'?'✓':'↔') ?></span><div><strong><?= htmlspecialchars((string)$task['title']) ?></strong><small><?= htmlspecialchars((string)$task['text']) ?></small></div><b>›</b></a><?php endforeach; ?></div>
    </article><?php endif; ?>
    <article class="pilot-panel pilot-wide">
        <header><div><span class="pilot-eyebrow">Planning</span><h3>Ateliers programmés</h3></div><?php if ($canGroups): ?><a href="/public/groupes">Voir les groupes →</a><?php endif; ?></header>
        <?php if (!$agenda): ?><div class="pilot-empty">📅<strong>Aucun atelier programmé</strong><p>Les prochains créneaux apparaîtront ici.</p></div>
        <?php else: ?><div class="pilot-agenda"><?php foreach ($agenda as $item): ?>
            <div class="pilot-agenda-row"><time><?= htmlspecialchars(substr((string)$item['heure_debut'],0,5)) ?></time><b><?= htmlspecialchars(ucfirst((string)$item['jour_semaine'])) ?></b><div><strong><?= htmlspecialchars((string)$item['libelle']) ?></strong><small><?= htmlspecialchars((string)$item['salle']) ?> · <?= htmlspecialchars(substr((string)$item['heure_debut'],0,5)) ?>–<?= htmlspecialchars(substr((string)$item['heure_fin'],0,5)) ?></small></div></div>
        <?php endforeach; ?></div><?php endif; ?>
    </article>

    <aside class="pilot-panel">
        <header><div><span class="pilot-eyebrow">Vigilance</span><h3>Alertes</h3></div></header>
        <div class="pilot-alerts"><?php foreach ($alerts as $alert): ?><div class="pilot-alert <?= htmlspecialchars((string)$alert['level']) ?>"><strong><?= htmlspecialchars((string)$alert['title']) ?></strong><span><?= htmlspecialchars((string)$alert['text']) ?></span></div><?php endforeach; ?></div>
    </aside>

    <?php if ($canClients): ?><article class="pilot-panel">
        <header><div><span class="pilot-eyebrow">Dossiers récents</span><h3>Nouveaux adhérents</h3></div><a href="/public/clients">Tous →</a></header>
        <?php if (!$recentClients): ?><div class="pilot-empty compact">👥<p>Aucun dossier récent.</p></div><?php else: ?><div class="pilot-people"><?php foreach ($recentClients as $client): ?><a href="/public/clients/voir?id=<?= (int)$client['id'] ?>"><span><?= htmlspecialchars(mb_strtoupper(mb_substr((string)$client['prenom'],0,1).mb_substr((string)$client['nom'],0,1))) ?></span><div><strong><?= htmlspecialchars((string)$client['prenom'].' '.(string)$client['nom']) ?></strong><small><?= htmlspecialchars((string)($client['email'] ?: 'Adresse mail non renseignée')) ?></small></div><b>›</b></a><?php endforeach; ?></div><?php endif; ?>
    </article><?php endif; ?>

    <?php if ($canWorkshops): ?><article class="pilot-panel">
        <header><div><span class="pilot-eyebrow">Capacité</span><h3>Remplissage</h3></div><a href="/public/ateliers">Détails →</a></header>
        <?php if (!$capacity): ?><div class="pilot-empty compact">📊<p>Aucune capacité exploitable.</p></div><?php else: ?><div class="pilot-capacity"><?php foreach (array_slice($capacity,0,6) as $row): $cap=max(0,(int)$row['capacite']);$ins=max(0,(int)$row['inscrits']);$rate=$cap>0?min(100,(int)round($ins/$cap*100)):0; ?><div><p><strong><?= htmlspecialchars((string)$row['libelle']) ?></strong><span><?= $ins ?> / <?= $cap ?: '—' ?></span></p><progress max="100" value="<?= $rate ?>"><?= $rate ?>%</progress></div><?php endforeach; ?></div><?php endif; ?>
    </article><?php endif; ?>
</section>
