const { pool } = require('../database') async function getAllConfigEcolage() { const sql = ` SELECT c.*, m.nom AS mention_nom, m.uniter AS mention_uniter, n.nom AS niveau_nom FROM configecolage c LEFT JOIN mentions m ON c.mention_id = m.id LEFT JOIN niveaus n ON c.niveau_id = n.id ORDER BY n.nom, m.nom ` try { const [rows] = await pool.query(sql) return rows } catch (error) { return [] } } async function getConfigEcolageByMentionNiveau(mention_id, niveau_nom) { const sql = ` SELECT c.montant_total FROM configecolage c LEFT JOIN niveaus n ON c.niveau_id = n.id WHERE c.mention_id = ? AND n.nom = ? ` try { const [rows] = await pool.query(sql, [mention_id, niveau_nom]) return rows.length > 0 ? rows[0] : null } catch (error) { return null } } async function upsertConfigEcolage(mention_id, niveau_id, montant_total) { const sql = ` INSERT INTO configecolage (mention_id, niveau_id, montant_total) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE montant_total = ? ` try { await pool.query(sql, [mention_id, niveau_id, montant_total, montant_total]) return { success: true } } catch (error) { return { success: false, error: error.message } } } async function deleteConfigEcolage(id) { try { await pool.query('DELETE FROM configecolage WHERE id = ?', [id]) return { success: true } } catch (error) { return { success: false, error: error.message } } } module.exports = { getAllConfigEcolage, getConfigEcolageByMentionNiveau, upsertConfigEcolage, deleteConfigEcolage }