import { Router } from 'express';
import pool from '../db.js';
const router = Router();
// Get settings
router.get('/', async (req, res) => {
    try {
        const [rows] = await pool.query('SELECT * FROM settings WHERE id = 1');
        if (rows.length === 0) {
            // Should be created by migration, but just in case
            const defaultSettings = {
                company_name: 'Golden Castel Construction',
                email: 'hello@goldencastel.com',
                phone: '+1 555-0101',
                address: '123 Business Avenue, Suite 500, New York, NY 10001',
                trn_number: '',
                vat_percentage: 5.00
            };
            await pool.query('INSERT IGNORE INTO settings (id, company_name, email, phone, address, trn_number, vat_percentage) VALUES (1, ?, ?, ?, ?, ?, ?)', [defaultSettings.company_name, defaultSettings.email, defaultSettings.phone, defaultSettings.address, defaultSettings.trn_number, defaultSettings.vat_percentage]);
            return res.json(defaultSettings);
        }
        res.json(rows[0]);
    }
    catch (error) {
        console.error(error);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});
// Update settings
router.put('/', async (req, res) => {
    const { company_name, email, phone, address, trn_number, vat_percentage, logo_url } = req.body;
    try {
        await pool.query(`UPDATE settings SET 
                company_name = ?, 
                email = ?, 
                phone = ?, 
                address = ?, 
                trn_number = ?, 
                vat_percentage = ?,
                logo_url = ?
            WHERE id = 1`, [company_name, email, phone, address, trn_number, vat_percentage, logo_url]);
        res.json({ message: 'Settings updated successfully' });
    }
    catch (error) {
        console.error(error);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});
export default router;
//# sourceMappingURL=settings.js.map