const { pool } = require('../database')

/**
 * function to get all Systeme note
 * @returns promise
 */
async function getSysteme() {
  const sql = 'SELECT * FROM notesystems'

  try {
    let [rows] = await pool.query(sql)

    return rows[0]
  } catch (error) {
    return error
  }
}

/**
 * function to update systeme note
 * @param {*} id
 * @param {*} admis
 * @param {*} redouble
 * @param {*} renvoyer
 * @returns promise
 */
async function updateSysteme(id, admis, redouble, renvoyer) {
  const sql = 'UPDATE notesystems SET admis = ?, redouble = ?, renvoyer = ? WHERE id = ?'

  try {
    let [result] = await pool.query(sql, [admis, redouble, renvoyer, id])

    if (result.affectedRows === 0) {
      return {
        success: false,
        message: 'Année Univesitaire non trouvé.'
      }
    }

    return {
      success: true,
      message: 'Année Univesitaire supprimé avec succès.'
    }
  } catch (error) {
    return error
  }
}

module.exports = {
  getSysteme,
  updateSysteme
}
