const express = require('express'); const pool = require('../config/database'); const { AppError } = require('../middleware/errorHandler'); const router = express.Router(); // @route GET /api/spreader-settings // @desc Get all spreader settings // @access Private router.get('/', async (req, res, next) => { try { const result = await pool.query( `SELECT * FROM spreader_settings ORDER BY spreader_brand, spreader_model NULLS LAST, setting_value` ); // Group by brand for easier frontend consumption const groupedSettings = result.rows.reduce((acc, setting) => { const brand = setting.spreader_brand; if (!acc[brand]) { acc[brand] = []; } acc[brand].push({ id: setting.id, model: setting.spreader_model, setting: setting.setting_value, description: setting.application_rate_description }); return acc; }, {}); res.json({ success: true, data: { settings: result.rows, groupedSettings } }); } catch (error) { next(error); } }); // @route GET /api/spreader-settings/brands // @desc Get list of spreader brands // @access Private router.get('/brands', async (req, res, next) => { try { const result = await pool.query( `SELECT DISTINCT spreader_brand as brand, COUNT(*) as setting_count FROM spreader_settings GROUP BY spreader_brand ORDER BY spreader_brand` ); res.json({ success: true, data: { brands: result.rows } }); } catch (error) { next(error); } }); // @route GET /api/spreader-settings/:brand // @desc Get settings for a specific brand // @access Private router.get('/:brand', async (req, res, next) => { try { const brand = req.params.brand; const result = await pool.query( `SELECT * FROM spreader_settings WHERE spreader_brand = $1 ORDER BY spreader_model NULLS LAST, setting_value`, [brand] ); res.json({ success: true, data: { brand, settings: result.rows } }); } catch (error) { next(error); } }); module.exports = router;