const express = require('express'); const router = express.Router(); const { getDb } = require('../db'); router.get('/', (req, res) => { try { const db = getDb(); const lowStock = db.prepare(` SELECT * FROM products WHERE low_stock_threshold > 0 AND quantity_on_hand <= low_stock_threshold ORDER BY quantity_on_hand ASC `).all(); const recentOrders = db.prepare(` SELECT o.*, c.name as customer_name FROM orders o LEFT JOIN customers c ON c.id = o.customer_id ORDER BY o.created_at DESC LIMIT 10 `).all(); const productCount = db.prepare('SELECT COUNT(*) as count FROM products').get().count; const customerCount = db.prepare('SELECT COUNT(*) as count FROM customers').get().count; const orderCount = db.prepare('SELECT COUNT(*) as count FROM orders').get().count; res.json({ lowStock, recentOrders, productCount, customerCount, orderCount, }); } catch (err) { res.status(500).json({ error: err.message }); } }); module.exports = router;