const express = require('express');
const router = express.Router();
const database = require('../config/database');
const { auth, isAdmin } = require('../middleware/auth');

router.get('/', async (req, res) => {
    try {
        const config = await database.all('SELECT * FROM pricing_config');
        const extras = await database.all('SELECT * FROM extras WHERE is_active = 1');
        res.json({ config, extras });
    } catch (error) {
        res.status(500).json({ error: 'Failed to fetch pricing' });
    }
});

router.put('/', auth, isAdmin, async (req, res) => {
    try {
        const { updates } = req.body;
        for (const [key, value] of Object.entries(updates)) {
            await database.run(
                'UPDATE pricing_config SET config_value = ?, updated_at = CURRENT_TIMESTAMP WHERE config_key = ?',
                [value, key]
            );
        }
        res.json({ message: 'Pricing updated successfully' });
    } catch (error) {
        res.status(500).json({ error: 'Failed to update pricing' });
    }
});

module.exports = router;
