/**
 * GET /api/pilotage-proactif
 * Pilotage proactif : référence, entrepreneur, projet, montant max/min/projeté, résultat
 */
const express = require('express');
const router  = express.Router();
const { getMarches } = require('../services/baserow');
const {
  selectVal, isCloture, normalizeMarche, parseNum,
  niveauAvancement, getDelaiRestant, niveauAlerte, resultatPhysique,
  SEUIL_STANDARD, SEUIL_MODERNISATION, SEUIL_CRITIQUE_PCT,
} = require('../services/calc');

router.get('/', async (req, res) => {
  try {
    const { region, entrepreneur, nature, niveau, resultat } = req.query;
    const regionFilter = req.regionFilter;

    let rows = await getMarches();
    rows = rows.filter(r => !isCloture(r));

    if (regionFilter) rows = rows.filter(r => r.region === regionFilter);
    else if (region)  rows = rows.filter(r => r.region === region);
    if (entrepreneur) rows = rows.filter(r => String(r.entrepreneur || '').toLowerCase().includes(entrepreneur.toLowerCase()));
    if (nature)       rows = rows.filter(r => selectVal(r.nature).toLowerCase().includes(nature.toLowerCase()));

    const items = rows.map(r => {
      const m      = normalizeMarche(r);
      const delai  = getDelaiRestant(r);
      return {
        ...m,
        delai_restant:     delai,
        niveau_alerte:     niveauAlerte(delai),
        niveau_avancement: niveauAvancement(r.taux_phy || r.avt_phy, selectVal(r.nature)),
        resultat:          resultatPhysique(r),
      };
    });

    const normal          = items.filter(r => r.resultat === 'Normal');
    const sous_avancement = items.filter(r => r.resultat === 'Sous Avancement');
    const depassement     = items.filter(r => r.resultat === 'Dépassement');
    const non_determine   = items.filter(r => r.resultat === 'Non déterminé');

    // Filtres optionnels
    let result = items;
    if (niveau === 'normal')      result = normal;
    else if (niveau === 'sous')   result = sous_avancement;
    else if (niveau === 'dep')    result = depassement;
    if (resultat)                 result = result.filter(r => r.resultat === resultat);

    res.json({
      seuils: {
        standard:      SEUIL_STANDARD,
        modernisation: SEUIL_MODERNISATION,
        critique:      SEUIL_CRITIQUE_PCT,
      },
      resume: {
        total:         items.length,
        normal:        normal.length,
        sous_avancement: sous_avancement.length,
        depassement:   depassement.length,
        non_determine: non_determine.length,
      },
      normal, sous_avancement, depassement, non_determine,
      items: result,
    });
  } catch (err) {
    res.status(502).json({ error: 'Erreur Baserow', detail: err.message });
  }
});

module.exports = router;
