const express = require('express');
const router = express.Router();
const facService = require('../services/facService');
const constantesJSON = require("../constantes/json-modulateur-fac");


router.post('/create', async (req, res) => {
  const data = req.body;
  const fac = await facService.createFAC(data);

  res.json({valid: Boolean(fac), fac});
});

router.post('/createProjet', async (req, res) => {
  const data = req.body;
  const fac = await facService.createFacProjet(data);

  res.json({valid: Boolean(fac), fac});
});

router.post('/createTarif', async (req, res) => {
  const data = req.body;
  const fac = await facService.createFacTarif(data);

  res.json({valid: Boolean(fac), fac});
});

router.get("/read/id/:id", async (req, res) => {
  const id = req.params.id;

  try {
    const fac = await facService.getFACbyId(id);
    res.json({valid: Boolean(fac), fac});
  } catch (error) {
    res.status(500).json({valid: false, error: "Internal Server Error"});
  }
});

router.get("/modulo/:objDemande", async (req, res) => {

  const objDemande = req.params.objDemande;
  var objRetourne

  switch (objDemande) {

    case "natureMar":
      objRetourne = constantesJSON.objModNatureMar;
      break;

    case "montantCA":
      objRetourne = constantesJSON.objModMontantCA;
      break;
    
    case "franchiseTousCas":
      objRetourne = constantesJSON.objModFranchiseTousCas;
      break;
    
    case "activite":
      objRetourne = constantesJSON.objModAct;
      break;
    
    case "conditionnement":
      objRetourne = constantesJSON.objModCond;
      break;
    
    case "zoneTransport":
      objRetourne = constantesJSON.objModZoneTransp;
      break;
    
    case "tppc":
      objRetourne = constantesJSON.objModTPPC;
      break;
    
    case "marEnExpo":
      objRetourne = constantesJSON.objMarEnExpo;
      break;
    
    case "rg":
      objRetourne = constantesJSON.objModRG;
      break;
  }

  try {
    res.json({valid: Boolean(objRetourne), objRetourne});
  } catch (error) {
    logger.log("error", `Error finding constant ${objDemande}:`, error);

    res.status(500).json({valid: false, error: "Internal Server Error"});
  }
});

module.exports = router;