/////////////////////////////////////////////////
// LOGGING
/////////////////////////////////////////////////
const logger = require("../../public/js/app/logger_concert");

/////////////////////////////////////////////////
// APPELS DES MODULES
/////////////////////////////////////////////////
const path = require("path");
const fs = require("fs");
// CALCUL DE DATE
const moment = require('moment');
// ENVOIE MAIL GOOGLE MAIL
const nodemailer = require('nodemailer');
// CONNECTION BASE DE DONNÉES
const mysql = require('mysql2');
const mysql_promise = require('mysql2/promise');

////////////////////////////////////////////////////
// FILE NAME
////////////////////////////////////////////////////
const filename = path.parse(__filename).base;

/////////////////////////////////////////////////
// APPELS DES FONCTIONS
/////////////////////////////////////////////////
const sleep = require("./../../modules/sleep");

////////////////////////////////////////////////////
// CONNECTION DB
////////////////////////////////////////////////////
async function connect_DB_promise() {
    return db_connection_promise = await mysql_promise.createConnection({
        host: process.env.db_connection_host,
        port: process.env.db_connection_port,
        user: process.env.db_connection_user,
        password: process.env.db_connection_password,
        database: process.env.db_connection_database
    });
}

/////////////////////////////////////////////////
// PAGE 1
// DOMAIN/concert/identifier
// 2. SAISIE DU L'IDENTIFIANT
// VÉRIFICATION SI L'IDENTIFIANT EST ADHÉRENT
/////////////////////////////////////////////////

const adminController = async function (req, res) {

    logger.info("======================================", { label: filename });
    logger.info("======== FUNCTION adminController ==========", { label: filename });
    logger.info("======================================", { label: filename });

    function affiche_page_admin() {
        ////////////////////////////////////////////////////
        // AFFIHER LA PAGE /identifier
        // AFFICHAGE DES ERREURS
        // EN CAS DE RETOUR SUR LA PAGE AVEC ERREUR :
        //  - SI IDENTIFIANT NON ADHÉRENT
        //  - SI RIEN SAISIE
        ////////////////////////////////////////////////////

        if (req.session.err_mail_form_adhesion_1) {
            res.locals.err_mail_form_adhesion_1 = req.session.err_mail_form_adhesion_1;
            res.locals.err_mail_form_adhesion_2 = req.session.err_mail_form_adhesion_2;
            res.locals.err_mail_form_adhesion_3 = req.session.err_mail_form_adhesion_3;
            // on supprime le contenu après affichage
            req.session.err_mail_form_adhesion_1 = undefined;
            req.session.err_mail_form_adhesion_2 = undefined;
            req.session.err_mail_form_adhesion_3 = undefined;
        }
        res.render('concert/page_templates/admin');
    };

    if (req.session.err_mail_form_adhesion_1) {
        res.locals.err_mail_form_adhesion_1 = req.session.err_mail_form_adhesion_1;
        res.locals.err_mail_form_adhesion_2 = req.session.err_mail_form_adhesion_2;
        res.locals.err_mail_form_adhesion_3 = req.session.err_mail_form_adhesion_3;
        // on supprime le contenu après affichage
        req.session.err_mail_form_adhesion_1 = undefined;
        req.session.err_mail_form_adhesion_2 = undefined;
        req.session.err_mail_form_adhesion_3 = undefined;
    }

    // CONNECTION DB PROMISE
    try {
        //Connexion
        let db_connection_promise = await connect_DB_promise();

        let sql_count_instruments = 'SELECT count(ca_concertiste.concertiste_intrument) AS concertistes_total FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_instruments, fields_count_instruments] = await db_connection_promise.execute(sql_count_instruments);
        console.log("results_count_instruments = ",results_count_instruments);

        let sql_count_piano = 'SELECT count(ca_concertiste.concertiste_intrument) AS piano FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Piano" AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_piano, fields_count_piano] = await db_connection_promise.execute(sql_count_piano);
        console.log("results_count_piano = ",results_count_piano);

        let sql_count_violon_1 = 'SELECT count(ca_concertiste.concertiste_intrument) AS violon_1 FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Violon" AND (ca_concertiste.concertiste_cahier = "Cahier 1" OR ca_concertiste.concertiste_cahier = "Cahier 2" OR ca_concertiste.concertiste_cahier = "Cahier 3" OR ca_concertiste.concertiste_cahier = "Cahier 4" OR ca_concertiste.concertiste_cahier = "Cahier 5") AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_violon_1, fields_count_violon_1] = await db_connection_promise.execute(sql_count_violon_1);
        console.log("results_count_violon_1 = ",results_count_violon_1);
        
        let sql_count_violon_2 = 'SELECT count(ca_concertiste.concertiste_intrument) AS violon_2 FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Violon" AND (ca_concertiste.concertiste_cahier = "Cahier 6" OR ca_concertiste.concertiste_cahier = "Cahier 7" OR ca_concertiste.concertiste_cahier = "Cahier 8" OR ca_concertiste.concertiste_cahier = "Cahier 9" OR ca_concertiste.concertiste_cahier = "Cahier 10") AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_violon_2, fields_count_violon_2] = await db_connection_promise.execute(sql_count_violon_2);
        console.log("results_count_violon_2 = ",results_count_violon_2);

        let sql_count_alto = 'SELECT count(ca_concertiste.concertiste_intrument) AS alto FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Alto" AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_alto, fields_count_alto] = await db_connection_promise.execute(sql_count_alto);
        console.log("results_count_alto = ",results_count_alto);

        let sql_count_violoncelle_1 = 'SELECT count(ca_concertiste.concertiste_intrument) AS violoncelle_1 FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Violoncelle" AND (ca_concertiste.concertiste_cahier = "Cahier 1" OR ca_concertiste.concertiste_cahier = "Cahier 2" OR ca_concertiste.concertiste_cahier = "Cahier 3" OR ca_concertiste.concertiste_cahier = "Cahier 4") AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_violoncelle_1, fields_count_violoncelle_1] = await db_connection_promise.execute(sql_count_violoncelle_1);
        console.log("results_count_violoncelle_1 = ",results_count_violoncelle_1);

        let sql_count_violoncelle_2 = 'SELECT count(ca_concertiste.concertiste_intrument) AS violoncelle_2 FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Violoncelle" AND (ca_concertiste.concertiste_cahier = "Cahier 5" OR ca_concertiste.concertiste_cahier = "Cahier 6" OR ca_concertiste.concertiste_cahier = "Cahier 7" OR ca_concertiste.concertiste_cahier = "Cahier 8" OR ca_concertiste.concertiste_cahier = "Cahier 9" OR ca_concertiste.concertiste_cahier = "Cahier 10") AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_violoncelle_2, fields_count_violoncelle_2] = await db_connection_promise.execute(sql_count_violoncelle_2);
        console.log("results_count_violoncelle_2 = ",results_count_violoncelle_2);
       
        let sql_count_trompette = 'SELECT count(ca_concertiste.concertiste_intrument) AS trompette FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Trompette" AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_trompette, fields_count_trompette] = await db_connection_promise.execute(sql_count_trompette);
        console.log("results_count_trompette = ",results_count_trompette);

        let sql_count_flute = 'SELECT count(ca_concertiste.concertiste_intrument) AS flute FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Flute" AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_flute, fields_count_flute] = await db_connection_promise.execute(sql_count_flute);
        console.log("results_count_flute = ",results_count_flute);

        let sql_count_harpe = 'SELECT count(ca_concertiste.concertiste_intrument) AS harpe FROM ca_concertiste LEFT JOIN ca_adherent ON ca_concertiste.identifiant = ca_adherent.identifiant WHERE ca_concertiste.concertiste_intrument = "Harpe" AND ca_adherent.stripe_statut_paiement = "paye"';
        var [results_count_harpe, fields_count_harpe] = await db_connection_promise.execute(sql_count_harpe);
        console.log("results_count_harpe = ",results_count_harpe);

        let sql_count_encadrant_prof = 'SELECT count(ca_encadrant.identifiant) AS encadrant_prof FROM ca_encadrant WHERE ca_encadrant.encadrant_type="Professeur"';
        var [results_count_encadrant_prof, fields_count_encadrant_prof] = await db_connection_promise.execute(sql_count_encadrant_prof);
        console.log("results_count_encadrant_prof = ",results_count_encadrant_prof);

        let sql_count_encadrant_parent = 'SELECT count(ca_encadrant.identifiant) AS encadrant_parent FROM ca_encadrant WHERE ca_encadrant.encadrant_type="Parent"';
        var [results_count_encadrant_parent, fields_count_encadrant_parent] = await db_connection_promise.execute(sql_count_encadrant_parent);
        console.log("results_count_encadrant_parent = ",results_count_encadrant_parent);

        let sql_count_spectateurs = 'SELECT sum(ca_adherent.commande_nb_spectateur) AS spectateurs FROM `ca_adherent`';
        var [results_count_spectateurs, fields_count_spectateurs] = await db_connection_promise.execute(sql_count_spectateurs);
        console.log("results_count_spectateurs = ",results_count_spectateurs);


    }  catch (error) {
        // Echec de connection à la BD
        logger.error("Il y a un problème avec la connection à la base de données! : " + error, { numsession: req.session.id });
        req.session.err_mail_form_adhesion_1 = '"Il y a un problème système connexion base de données, veuillez recommencer, "' + error;
        //await db_connection_promise.end();
        affiche_page_identifier();
    }





    // affichage du template ejs /views/page_templates/inscription_accueil.ejs

    //  results_count_instruments = { name: 'John', age: 30, job: 'Developer' };
    res.render('concert/page_templates/admin', { 
        results_count_instruments: results_count_instruments[0],
        results_count_piano: results_count_piano[0],
        results_count_violon_1: results_count_violon_1[0],
        results_count_violon_2: results_count_violon_2[0],
        results_count_violoncelle_1: results_count_violoncelle_1[0],
        results_count_violoncelle_2: results_count_violoncelle_2[0],
        results_count_trompette: results_count_trompette[0],
        results_count_alto: results_count_alto[0],
        results_count_flute: results_count_flute[0],
        results_count_harpe: results_count_harpe[0],
        results_count_encadrant_prof: results_count_encadrant_prof[0],
        results_count_encadrant_parent: results_count_encadrant_parent[0],
        results_count_spectateurs: results_count_spectateurs[0]
    });




}

/////////////////////////////////////////////////
// EXPORT DES MODULES POUR QU'ILS PUISSENT LUS PAR LES AUTRES FICHIERS JS
/////////////////////////////////////////////////

module.exports = {
    adminController
}
