const express = require('express'); const router = express.Router(); const { pool } = require('../db'); const { getBabyAgeContext } = require('../pediatricEngine'); // GET /api/babies - List all babies (supports optional ?family_id= filter) router.get('/', async (req, res) => { try { const { family_id } = req.query; let query = 'SELECT * FROM `baby_profile`'; const params = []; if (family_id) { query += ' WHERE `family_id` = ?'; params.push(family_id); } query += ' ORDER BY `is_default` DESC, `id` ASC'; const [rows] = await pool.query(query, params); const babiesWithContext = rows.map(b => { const ageContext = getBabyAgeContext(b.birth_date); return { ...b, ageDays: ageContext.diffDays, ageMonths: ageContext.months, ageText: ageContext.ageText, baseline: ageContext.baseline }; }); res.json({ success: true, data: babiesWithContext }); } catch (err) { console.error('Error fetching babies:', err); res.status(500).json({ success: false, error: err.message }); } }); // POST /api/babies - Create or update baby profile router.post('/', async (req, res) => { const { babyId, familyId, name, gender, birthDate, themeColor } = req.body; if (!name || !birthDate) { return res.status(400).json({ success: false, error: '姓名与出生日期必填' }); } const id = babyId || 'baby-' + Date.now(); const color = themeColor || '#3B82F6'; const targetFamilyId = familyId || 'family-001'; try { if (babyId) { // Update await pool.query(` UPDATE \`baby_profile\` SET \`name\` = ?, \`gender\` = ?, \`birth_date\` = ?, \`theme_color\` = ? WHERE \`baby_id\` = ? `, [name, gender || 'boy', birthDate, color, babyId]); } else { // Insert with family_id await pool.query(` INSERT INTO \`baby_profile\` (\`baby_id\`, \`family_id\`, \`name\`, \`gender\`, \`birth_date\`, \`theme_color\`, \`is_default\`) VALUES (?, ?, ?, ?, ?, ?, 0) `, [id, targetFamilyId, name, gender || 'boy', birthDate, color]); } const [updated] = await pool.query('SELECT * FROM `baby_profile` WHERE `baby_id` = ?', [id]); res.json({ success: true, data: updated[0] }); } catch (err) { console.error('Error saving baby:', err); res.status(500).json({ success: false, error: err.message }); } }); // PUT /api/babies/:babyId - Update baby profile router.put('/:babyId', async (req, res) => { const { babyId } = req.params; const { name, gender, birthDate, themeColor, defaultConfig } = req.body; if (!name || !birthDate) { return res.status(400).json({ success: false, error: '姓名与出生日期必填' }); } const color = themeColor || '#3B82F6'; try { const configJson = defaultConfig ? (typeof defaultConfig === 'string' ? defaultConfig : JSON.stringify(defaultConfig)) : null; if (configJson !== null) { await pool.query(` UPDATE \`baby_profile\` SET \`name\` = ?, \`gender\` = ?, \`birth_date\` = ?, \`theme_color\` = ?, \`default_config\` = ? WHERE \`baby_id\` = ? `, [name, gender || 'boy', birthDate, color, configJson, babyId]); } else { await pool.query(` UPDATE \`baby_profile\` SET \`name\` = ?, \`gender\` = ?, \`birth_date\` = ?, \`theme_color\` = ? WHERE \`baby_id\` = ? `, [name, gender || 'boy', birthDate, color, babyId]); } const [updated] = await pool.query('SELECT * FROM `baby_profile` WHERE `baby_id` = ?', [babyId]); res.json({ success: true, data: updated[0] }); } catch (err) { console.error('Error updating baby:', err); res.status(500).json({ success: false, error: err.message }); } }); // PUT /api/babies/:babyId/defaults - Update default config for baby router.put('/:babyId/defaults', async (req, res) => { const { babyId } = req.params; const { defaultConfig } = req.body; try { const configJson = typeof defaultConfig === 'string' ? defaultConfig : JSON.stringify(defaultConfig || {}); await pool.query('UPDATE `baby_profile` SET `default_config` = ? WHERE `baby_id` = ?', [configJson, babyId]); const [updated] = await pool.query('SELECT * FROM `baby_profile` WHERE `baby_id` = ?', [babyId]); res.json({ success: true, message: '宝宝默认行为配置已更新', data: updated[0] }); } catch (err) { console.error('Error updating baby defaults:', err); res.status(500).json({ success: false, error: err.message }); } }); // POST /api/babies/:babyId/default - Set as default baby router.post('/:babyId/default', async (req, res) => { const { babyId } = req.params; try { await pool.query('UPDATE `baby_profile` SET `is_default` = 0'); await pool.query('UPDATE `baby_profile` SET `is_default` = 1 WHERE `baby_id` = ?', [babyId]); res.json({ success: true }); } catch (err) { res.status(500).json({ success: false, error: err.message }); } }); module.exports = router;