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

/**
 * function to insert etudiant into databases
 */
async function insertEtudiant(
  nom,
  prenom,
  photos,
  date_de_naissances,
  niveau,
  annee_scolaire,
  status,
  num_inscription,
  mention_id,
  sexe,
  nationaliter,
  cin,
  date_delivrence,
  annee_bacc,
  serie,
  boursier,
  domaine,
  contact,
  parcours
) {
  const query = database.prepare(
    'INSERT INTO etudiants (nom, prenom, photos, date_de_naissances, niveau, annee_scolaire, status, mention_id, num_inscription, sexe, cin, date_delivrance, nationalite, annee_bacc, serie, boursier, domaine, contact, parcours) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  )

  try {
    let response = await query.run(
      nom,
      prenom,
      photos,
      date_de_naissances,
      niveau,
      annee_scolaire,
      status,
      mention_id,
      num_inscription,
      sexe,
      cin,
      date_delivrence,
      nationaliter,
      annee_bacc,
      serie,
      boursier,
      domaine,
      contact,
      parcours
    )
    return response
  } catch (error) {
    return error
  }
}

/**
 * function to get all etudiants
 *
 * @returns JSON
 */
async function getAllEtudiants() {
  const query = database.prepare('SELECT * FROM etudiants ORDER BY annee_scolaire DESC')
  try {
    let response = await query.all()

    return response
  } catch (error) {
    return error
  }
}

/**
 * function to return a single etudiant
 * and display it on the screen
 *
 * @param {int} id
 * @returns Promise
 */
async function getSingleEtudiant(id) {
  const query = database.prepare('SELECT * FROM etudiants WHERE id = ?')

  try {
    const etudiants = await query.get(id)

    if (etudiants) {
      return etudiants
    } else {
      return { message: 'etudiants pas trouver' }
    }
  } catch (error) {
    return error
  }
}

/**
 * function to get all etudiants M2
 *
 * @returns JSON
 */
async function FilterDataByNiveau(niveau) {
  const query = database.prepare(
    'SELECT * FROM etudiants WHERE niveau = ? ORDER BY annee_scolaire DESC'
  )
  try {
    let response = await query.all(niveau)

    return response
  } catch (error) {
    return error
  }
}

/**
 * function to update etudiants
 *
 * @param {*} nom
 * @param {*} prenom
 * @param {*} photos
 * @param {*} date_de_naissances
 * @param {*} niveau
 * @param {*} annee_scolaire
 * @param {*} status
 * @param {*} num_inscription
 * @param {*} id
 * @returns promise
 */
async function updateEtudiant(
  nom,
  prenom,
  photos,
  date_de_naissances,
  niveau,
  annee_scolaire,
  status,
  mention_id,
  num_inscription,
  id,
  sexe,
  nationalite,
  cin,
  date_delivrence,
  annee_bacc,
  serie,
  boursier,
  domaine,
  contact,
  parcours
) {
  const query = database.prepare(
    'UPDATE etudiants SET nom = ?, prenom = ?, photos = ?, date_de_naissances = ?, niveau = ?, annee_scolaire = ?, status = ?, mention_id = ?, num_inscription = ?, sexe = ?, cin = ?, date_delivrance = ?, nationalite = ?, annee_bacc = ?, serie = ?, boursier = ?, domaine = ?, contact = ?, parcours = ? WHERE id = ?'
  )

  try {
    let response = await query.run(
      nom,
      prenom,
      photos,
      date_de_naissances,
      niveau,
      annee_scolaire,
      status,
      mention_id,
      num_inscription,
      sexe,
      cin,
      date_delivrence,
      nationalite,
      annee_bacc,
      serie,
      boursier,
      domaine,
      contact,
      parcours,
      id
    )

    return response
  } catch (error) {
    return error
  }
}

/**
 * function to return the needed data in dashboard
 *
 * @returns promise
 */
async function getDataToDashboard() {
  const query = database.prepare('SELECT * FROM niveaus')
  const query2 = database.prepare('SELECT * FROM etudiants')
  const query3 = database.prepare('SELECT DISTINCT annee_scolaire FROM etudiants') // get all Année Univesitaire sans doublan

  try {
    let niveau = query.all()
    let etudiants = query2.all()
    let anne_scolaire = query3.all()

    return { niveau, etudiants, anne_scolaire }
  } catch (error) {
    return error
  }
}

async function changePDP(photos, id) {
  const query = database.prepare('UPDATE etudiants SET photos = ? WHERE id = ?')

  try {
    let response = await query.run(photos, id)

    return response
  } catch (error) {
    return error
  }
}

async function updateParcours(parcours, id) {
  const query = database.prepare('UPDATE etudiants SET parcours = ? WHERE id = ?')

  try {
    let response = await query.run(parcours, id)

    return response
  } catch (error) {
    return error
  }
}

async function createTranche(etudiant_id, tranchename, montant) {
  const query = database.prepare(
    'INSERT INTO trancheecolage (etudiant_id, tranchename, montant) VALUES (?, ?, ?)'
  )

  try {
    let response = query.run(etudiant_id, tranchename, montant)

    return response
  } catch (error) {
    return error
  }
}

async function getTranche(id) {
  const query = database.prepare('SELECT * FROM trancheecolage WHERE etudiant_id = ?')

  try {
    let response = query.all(id)

    return response
  } catch (error) {
    return error
  }
}

async function updateTranche(id, tranchename, montant) {
  const query = database.prepare(
    'UPDATE trancheecolage SET tranchename = ?, montant = ? WHERE id = ?'
  )

  try {
    let response = query.run(tranchename, montant, id)

    return response
  } catch (error) {
    return error
  }
}

async function deleteTranche(id) {
  const query = database.prepare('DELETE FROM trancheecolage WHERE id = ?')

  try {
    let response = query.run(id)

    return response
  } catch (error) {
    return error
  }
}

async function deleteEtudiant(id) {
  const query = database.prepare('DELETE FROM etudiants WHERE id = ?')

  try {
    let response = query.run(id)

    return response
  } catch (error) {
    return error
  }
}

async function getSingleTranche(id) {
  try {
    return await database.prepare('SELECT * FROM trancheecolage WHERE id = ?').get(id)
  } catch (error) {
    return error
  }
}

module.exports = {
  insertEtudiant,
  getAllEtudiants,
  FilterDataByNiveau,
  getSingleEtudiant,
  updateEtudiant,
  getDataToDashboard,
  changePDP,
  updateParcours,
  createTranche,
  getTranche,
  updateTranche,
  deleteTranche,
  deleteEtudiant,
  getSingleTranche
}
