const express = require('express');
const router = express.Router();
const { getMarches, getMarcheById } = require('../services/baserow');

// GET /api/marches
router.get('/', async (req, res) => {
  try {
    const rows = await getMarches();
    res.json({ count: rows.length, results: rows });
  } catch (err) {
    res.status(502).json({ error: 'Erreur Baserow', detail: err.message });
  }
});

// GET /api/marches/:id
router.get('/:id', async (req, res) => {
  try {
    const row = await getMarcheById(req.params.id);
    res.json(row);
  } catch (err) {
    const status = err.response?.status === 404 ? 404 : 502;
    res.status(status).json({ error: 'Marché introuvable', detail: err.message });
  }
});

module.exports = router;
