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


router.post('/create', async (req, res) => {
    const data = req.body;
    const tppc = await tppcService.createTppc(data);

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

router.post('/createTarif', async (req, res) => {
    const data = req.body;
    const tarif = await tppcService.createTppcTarif(data);

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

router.post('/createProjet', async (req, res) => {
    const data = req.body;
    const projet = await tppcService.createTppcProjet(data);

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

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

    const objDemande = req.params.objDemande;
    var objRetourne

    switch (objDemande) {
        case "activite":
            objRetourne = constantesJSON.objModAct;
            break;
        case "marchandise":
            objRetourne = constantesJSON.objModMar;
            break;
        case "sinistre":
            objRetourne = constantesJSON.objModSinistre;
            break;
        case "cotisation":
            objRetourne = constantesJSON.objModCot;
            break;
        case "primeMini":
            objRetourne = constantesJSON.objPrimeMini;
            break;
        case "flotte":
            objRetourne = constantesJSON.objModFlotte;
            break;
        case "marEnExpo":
            objRetourne = constantesJSON.objMarEnExpo;
            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"});
    }
});


router.get("/list/vehicule", async (req, res) => {

    const objRetourne = constantesJSON.listTypeVehicule
    try {
        res.json({valid: Boolean(objRetourne), objRetourne});
    } catch (error) {
        logger.log("error", `Error finding constant "listTypeVehicule":`, error);

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


module.exports = router;