<?php
use App\Core\Csrf;
use App\Core\Permission;
?>
<link rel="stylesheet" href="/public/assets/css/cda-v5-2-1.css">
<link rel="stylesheet" href="/public/assets/css/cda-workshops-planning-956.css">
<link rel="stylesheet" href="/public/assets/css/cda-workshop-enrollment-counts-9677.css">

<section class="page-intro">
<div>
<span class="page-kicker">Catalogue pédagogique</span>
<h2>Cours et ateliers</h2>
<p>Les publics autorisés et les actions de gestion apparaissent sur chaque cours.</p>
</div>
<div class="workshop-toolbar">
<a class="button button-secondary" href="/public/groupes">Planning des groupes</a>
<?php if (Permission::can('axiom.planning.view')): ?>
<a class="button button-secondary" href="/public/administration/axiom/planning">Agenda AXIOM</a>
<?php endif; ?>
<?php if (Permission::can('ateliers.create')): ?>
<a class="button button-primary" href="/public/ateliers/nouveau">+ Nouveau cours</a>
<?php endif; ?>
</div>
</section>

<?php if (!empty($_SESSION['success'])): ?>
<div class="alert alert-success">
<?= htmlspecialchars((string)$_SESSION['success']) ?>
</div>
<?php unset($_SESSION['success']); endif; ?>

<?php if (empty($workshops)): ?>
<section class="card empty-state">
<h3>Aucun cours enregistré</h3>
<p>Créez le premier cours depuis le bouton « Nouveau cours ».</p>
</section>
<?php else: ?>

<section class="course-management-grid">
<?php foreach ($workshops as $workshop): ?>
<?php
$codes = array_filter(
    explode(',', (string)($workshop['public_codes'] ?? ''))
);

$labels = (string)(
    $workshop['public_libelles']
    ?? $workshop['publics']
    ?? ''
);

$isMixed =
    in_array('ENF', $codes, true)
    && in_array('ADU', $codes, true);

if (!$codes && str_contains(mb_strtolower($labels), 'enfant')
    && str_contains(mb_strtolower($labels), 'adulte')) {
    $isMixed = true;
}
?>

<article class="course-management-card">
<header>
<div class="course-main">
<div class="course-icon">
<?= ($workshop['type_atelier'] ?? '') === 'individuel' ? '🎹' : '🎨' ?>
</div>

<div>
<span class="course-pole">
<?= htmlspecialchars((string)($workshop['pole_libelle'] ?? 'Pôle non renseigné')) ?>
</span>

<h3>
<?= htmlspecialchars((string)($workshop['libelle'] ?? 'Cours')) ?>
</h3>

<p>
<?= htmlspecialchars((string)($workshop['discipline_libelle'] ?? 'Discipline non renseignée')) ?>
</p>
</div>
</div>

<b class="course-code">
<?= htmlspecialchars((string)($workshop['code'] ?? '—')) ?>
</b>
</header>

<div class="course-audiences">
<?php if ($isMixed): ?>
<span class="audience mix">🧒 + 👤 Mixte</span>
<?php else: ?>
    <?php if (in_array('ENF', $codes, true) || mb_strtolower($labels) === 'enfant'): ?>
    <span class="audience child">🧒 Enfant</span>
    <?php endif; ?>

    <?php if (in_array('ADU', $codes, true) || mb_strtolower($labels) === 'adulte'): ?>
    <span class="audience adult">👤 Adulte</span>
    <?php endif; ?>

    <?php if (!$codes && $labels === ''): ?>
    <span class="audience missing">Public non renseigné</span>
    <?php endif; ?>
<?php endif; ?>
</div>

<div class="course-metrics">
<div>
<strong><?= (int)($workshop['nb_groupes'] ?? 0) ?></strong>
<span>groupe(s)</span>
</div>

<div>
<strong>
<?= !empty($workshop['duree_standard_minutes'])
    ? (int)$workshop['duree_standard_minutes'].' min'
    : '—' ?>
</strong>
<span>durée</span>
</div>

<div>
<strong>
<?= !empty($workshop['capacite_max'])
    ? (int)$workshop['capacite_max']
    : '—' ?>
</strong>
<span>capacité</span>
</div>

<div>
<?php
$capacity = (int)($workshop['capacite_max'] ?? 0);
$registered = (int)($workshop['nb_inscrits'] ?? 0);
$remaining = $capacity > 0 ? max(0, $capacity - $registered) : null;
?>
<strong><?= $remaining === null ? '—' : $remaining ?></strong>
<span>place(s) restante(s)</span>
</div>
</div>

<div class="course-enrollment-status">
<span><strong><?= (int)($workshop['nb_inscrits'] ?? 0) ?></strong> élève(s) inscrit(s)</span>
<span class="<?= (int)($workshop['nb_demandes_attente'] ?? 0)>0?'has-waiting':'' ?>"><strong><?= (int)($workshop['nb_demandes_attente'] ?? 0) ?></strong> demande(s) en attente</span>
</div>

<?php if (!empty($workshop['prochain_creneau'])): ?>
<p class="course-next-slot">📅 <?= htmlspecialchars((string)$workshop['prochain_creneau']) ?></p>
<?php endif; ?>

<footer class="course-actions">
<span class="course-type">
<?= htmlspecialchars((string)($workshop['type_atelier'] ?? 'Non renseigné')) ?>
</span>

<div>
<a href="/public/groupes">📅 Groupes & créneaux</a>
<?php if (Permission::can('ateliers.edit')): ?>
<a class="action-edit"
href="/public/ateliers/modifier?id=<?= (int)$workshop['id'] ?>">
✎ Modifier
</a>
<?php endif; ?>

<a href="/public/tarifs">
€ Tarif
</a>

<?php if (Permission::can('ateliers.archive')): ?>
<form method="post"
action="/public/ateliers/archiver"
onsubmit="return confirm('Archiver ce cours ? Les groupes, inscriptions et historiques seront conservés.');">
<?= Csrf::field() ?>
<input type="hidden" name="id" value="<?= (int)$workshop['id'] ?>">
<button type="submit">Archiver</button>
</form>
<?php endif; ?>
</div>
</footer>
</article>
<?php endforeach; ?>
</section>
<?php endif; ?>
