const express = require('express');
const router = express.Router();
const rcService = require('../services/rcService');
const constantesJSON = require("../constantes/json-modulateur-rc");
const logger = require('../utils/logger');

// ===== Routes RC principale =====
router.post('/create', async (req, res) => {
    try {
        const data = req.body;
        const rc = await rcService.createRc(data);
        res.json({ valid: Boolean(rc), rc });
    } catch (error) {
        logger.log("error", "Error creating RC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

router.get('/:id', async (req, res) => {
    try {
        const rc = await rcService.getRcById(req.params.id);
        res.json({ valid: Boolean(rc), rc });
    } catch (error) {
        logger.log("error", "Error getting RC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

router.post('/update/:id', async (req, res) => {
    try {
        const rc = await rcService.updateRc(req.params.id, req.body);
        res.json({ valid: Boolean(rc), rc });
    } catch (error) {
        logger.log("error", "Error updating RC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

// ===== Routes TarifRC =====
router.post('/tarif/create', async (req, res) => {
    try {
        const data = req.body;
        const tarifRc = await rcService.createTarifRc(data);
        res.json({ valid: Boolean(tarifRc), tarifRc });
    } catch (error) {
        logger.log("error", "Error creating TarifRC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

router.get('/tarif/:id', async (req, res) => {
    try {
        const tarifRc = await rcService.getTarifRcById(req.params.id);
        res.json({ valid: Boolean(tarifRc), tarifRc });
    } catch (error) {
        logger.log("error", "Error getting TarifRC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

router.post('/tarif/update/:id', async (req, res) => {
    try {
        const tarifRc = await rcService.updateTarifRc(req.params.id, req.body);
        res.json({ valid: Boolean(tarifRc), tarifRc });
    } catch (error) {
        logger.log("error", "Error updating TarifRC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

// ===== Routes ProjetRC =====
router.post('/projet/create', async (req, res) => {
    try {
        const data = req.body;
        const projetRc = await rcService.createProjetRc(data);
        res.json({ valid: Boolean(projetRc), projetRc });
    } catch (error) {
        logger.log("error", "Error creating ProjetRC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

router.get('/projet/:id', async (req, res) => {
    try {
        const projetRc = await rcService.getProjetRcById(req.params.id);
        res.json({ valid: Boolean(projetRc), projetRc });
    } catch (error) {
        logger.log("error", "Error getting ProjetRC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

router.post('/projet/update/:id', async (req, res) => {
    try {
        const projetRc = await rcService.updateProjetRc(req.params.id, req.body);
        res.json({ valid: Boolean(projetRc), projetRc });
    } catch (error) {
        logger.log("error", "Error updating ProjetRC:", error);
        res.status(500).json({ valid: false, error: "Internal Server Error" });
    }
});

// ===== Routes Modulateurs =====
router.get("/modulo/:objDemande", async (req, res) => {
    const objDemande = req.params.objDemande;
    var objRetourne

    switch (objDemande) {
        case "CARC":
            objRetourne = constantesJSON.modRCCA;
            break;
        case "activiteRCC":
            objRetourne = constantesJSON.modRCActRCC;
            break;
        case "activiteRCE":
            objRetourne = constantesJSON.modRCActRCE;
            break;
        case "activiteComplRC":
            objRetourne = constantesJSON.modRCActCompl;
            break;
        case "marchandiseRC":
            objRetourne = constantesJSON.modRCMar;
            break;
        case "zoneRC":
            objRetourne = constantesJSON.modRCZone;
            break;
        case "engagComplRC":
            objRetourne = constantesJSON.modRCEngagCompl;
            break;
        case "garAdditionelRC":
            objRetourne = constantesJSON.modRCGarAdd ;
            break;
        case "sinistreRC":
            objRetourne = constantesJSON.modRCSinistre;
            break;
        case "franchiseRC":
            objRetourne = constantesJSON.modRCFranchise;
            break;
        case "primeMiniRC":
            objRetourne = constantesJSON.modRCPrimeMini;
            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;