/**
 * GET /api/en-cours
 * Marchés en cours (actifs, progress < 100%)
 */
const express = require('express');
const router  = express.Router();
const { getMarches } = require('../services/baserow');
const { isCloture, normalizeMarche, parseNum } = require('../services/calc');

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

    let rows = await getMarches();

    // Actifs dont taux_phy < 100
    rows = rows.filter(r => {
      if (isCloture(r)) return false;
      const t = parseNum(r.taux_phy ?? r.avt_phy);
      return t < 100;
    });

    // Filtres
    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 (projet)       rows = rows.filter(r => String(r.projet || '').toLowerCase().includes(projet.toLowerCase()));
    if (nature)       rows = rows.filter(r => String(r.nature || '').toLowerCase().includes(nature.toLowerCase()));
    if (statut)       rows = rows.filter(r => String(r.statut || '').toLowerCase().includes(statut.toLowerCase()));

    const items = rows.map(normalizeMarche);

    const tauxList  = items.map(r => r.taux_phy_raw).filter(v => v > 0);
    const tauxMoyen = tauxList.length
      ? Math.round(tauxList.reduce((a, b) => a + b, 0) / tauxList.length * 10) / 10
      : 0;

    res.json({
      count:  items.length,
      taux_avancement_moyen: tauxMoyen,
      items,
    });
  } catch (err) {
    res.status(502).json({ error: 'Erreur Baserow', detail: err.message });
  }
});

module.exports = router;
