/////////////////////////////////////////////////
// APPELS DES MODULES
/////////////////////////////////////////////////
const path = require("path");
const stripe = require('stripe')(process.env.STRIPE_PRIVATE_KEY);


/////////////////////////////////////////////////
// APPELS DES MODULES PERSOS
/////////////////////////////////////////////////
// loggings
const logger = require("../public/js/app/logger_whstripe");
// DB
const { connect_DB_promise } = require('../modules/db/databaseconnect');
const { generateInsertSQL, extractValues } = require('../modules/db/sqlqueries');

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

// Controllers
const { wh_stripe_don } = require("./donController");
const { wh_stripe_vente_video } = require("./vente_video/generate_linkController");



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


/////////////////////////////////////////////////
// RECEPTION DU WEBHOOK STRIPE wh_stripe
/////////////////////////////////////////////////
const wh_stripe = async function(req, res) {

  logger.info("======================================");
  logger.info("======== FUNCTION wh_stripe ==========");
  logger.info("======================================");

  const sig = req.headers['stripe-signature'];
  logger.info("sig = " + sig, { numsession: req.session.id, label: filename});

  let event;
  const endpointSecret = process.env.STRIPE_WEBHOOK_CLE_SIGNATURE_SECRETE;

  try {
    event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
  } catch (err) {
    logger.error("Webhook Error: " + err.message, { numsession: req.session.id, label: filename});
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Gérer les événements webhook
  
  logger.info("event.type = " + event.type, { numsession: req.session.id, label: filename});
  logger.info("event.data.object.id = " + event.data.object.id, { numsession: req.session.id, label: filename});
  logger.info("event.data.object.app = " + event.data.object.metadata.app, { numsession: req.session.id, label: filename});
  logger.info("event.data.object.customer_email = " + event.data.object.customer_email, { numsession: req.session.id, label: filename});
  logger.info("event.data.object.payment_status = " + event.data.object.payment_status, { numsession: req.session.id, label: filename});
  logger.info("event.data.object.metadata.identifiant = " + event.data.object.metadata.identifiant, { numsession: req.session.id, label: filename});
  logger.info("event.data.object.metadata.nom = " + event.data.object.metadata.nom, { numsession: req.session.id, label: filename});
  logger.info("event.data.object.metadata.montant = " + event.data.object.metadata.montant, { numsession: req.session.id, label: filename});

  logger.info("event.data.object.status = " + event.data.object.status, { numsession: req.session.id, label: filename});


  /////////////////////////////////////////////////
  // DON
  /////////////////////////////////////////////////

  switch (event.data.object.metadata.app) {
    case "don":
      logger.info("WEBHOOK wh_stripe => redirection vers DON wh_stripe_don", { numsession: req.session.id, label: filename});
      return wh_stripe_don(req, res, event);

    case "vente_video":
      logger.info("WEBHOOK wh_stripe => redirection vers VENTE VIDEO wh_stripe_vente_video", { numsession: req.session.id, label: filename});
      return wh_stripe_vente_video(req, res, event);

    default:
      // cas undefined, null, ou autre valeur non prévue
      return res.json({ received: true });
  }

}



/////////////////////////////////////////////////
// EXPORT DE MODULE POUR QU'IL SOIT LU DANS LES AUTRES FICHIERS JS
/////////////////////////////////////////////////
module.exports = {
    wh_stripe
}