<br />
<b>Warning</b>:  Undefined variable $auth in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>/home/pevo0181/public_html/pia-soft.com/cleania/routes/index.php</b> on line <b>542</b><br />
<?php
// ============================================================
//  GET /api/dashboard.php
//  Header: Authorization: Bearer <token>
// ============================================================
require_once __DIR__ . '/config.php';

$auth = requireAuth();
$eleve_id       = $auth['eleve_id'];
$inscription_id = $auth['inscription_id'];
$db = getDB();

// --- Moyenne générale (dernière session) ---
$stmtMoy = $db->prepare("
    SELECT ROUND(SUM(n.note * m.coefficient) / SUM(m.coefficient), 2) AS moyenne,
           COUNT(n.id) as nb_notes,
           se.nom_fr as session_nom
    FROM notes n
    JOIN matieres m ON m.id = n.matiere_id
    JOIN sessions_evaluation se ON se.id = n.session_id
    WHERE n.eleve_id = ?
      AND n.absent = 0
      AND n.session_id = (
          SELECT id FROM sessions_evaluation ORDER BY ordre DESC LIMIT 1
      )
    GROUP BY n.session_id
    LIMIT 1
");
$stmtMoy->execute([$eleve_id]);
$moy = $stmtMoy->fetch();

// --- Absences ce trimestre ---
$stmtAbs = $db->prepare("
    SELECT COUNT(*) as total,
           SUM(CASE WHEN statut='absent' THEN 1 ELSE 0 END) as absences,
           SUM(CASE WHEN statut='retard' THEN 1 ELSE 0 END) as retards
    FROM presences
    WHERE eleve_id = ?
      AND date_appel >= DATE_SUB(CURDATE(), INTERVAL 90 DAY)
");
$stmtAbs->execute([$eleve_id]);
$presences = $stmtAbs->fetch();

// --- Paiements pension (par eleve_id pour couvrir toutes ses inscriptions) ---
$stmtPay = $db->prepare("
    SELECT
        COALESCE(SUM(pp.montant_paye), 0) as total_paye,
        COALESCE((
            SELECT pc2.montant_total
            FROM pension_config pc2
            JOIN inscriptions i2 ON i2.eleve_id = ?
            JOIN classes cl2 ON cl2.id = i2.classe_id
            WHERE pc2.niveau_id = cl2.niveau_id
            ORDER BY i2.id DESC, pc2.annee_id DESC
            LIMIT 1
        ), 0) as montant_total,
        COUNT(pp.id) as nb_paiements
    FROM pension_paiements pp
    WHERE pp.eleve_id = ?
");
$stmtPay->execute([$eleve_id, $eleve_id]);
$paiement = $stmtPay->fetch();

// --- Notifications non lues ---
$stmtNotif = $db->prepare("
    SELECT (
        SELECT COUNT(*) FROM mobile_notifications WHERE eleve_id=? AND lu=0
    ) + (
        SELECT COUNT(*) FROM notifications WHERE eleve_id=? AND lu=0
    ) as non_lues
");
$stmtNotif->execute([$eleve_id, $eleve_id]);
$notifs = $stmtNotif->fetch();

// --- Dernières notifications (3) depuis les deux tables ---
$stmtDernNotif = $db->prepare("
    SELECT id, titre, message, titre_en, message_en, type, lu, created_at FROM (
        SELECT id, titre, message, titre as titre_en, message as message_en, type, lu, created_at
        FROM mobile_notifications
        WHERE eleve_id = ?
        UNION ALL
        SELECT id, titre_fr as titre, message_fr as message, titre_en, message_en, type, lu, created_at
        FROM notifications
        WHERE eleve_id = ?
    ) AS toutes
    ORDER BY created_at DESC, id DESC
    LIMIT 3
");
$stmtDernNotif->execute([$eleve_id, $eleve_id]);
$dernieres_notifs = $stmtDernNotif->fetchAll();

// --- Dernière arrivée ---
$stmtArr = $db->prepare("
    SELECT datetime FROM arrivee_ecole_eleve
    WHERE eleve_id = ?
    ORDER BY datetime DESC LIMIT 1
");
$stmtArr->execute([$eleve_id]);
$arrivee = $stmtArr->fetch();

jsonOk([
    'moyenne'     => $moy['moyenne'] ?? null,
    'session'     => $moy['session_nom'] ?? null,
    'presences'   => [
        'absences' => (int)($presences['absences'] ?? 0),
        'retards'  => (int)($presences['retards'] ?? 0),
    ],
    'paiement'    => [
        'total_paye'   => (float)($paiement