import { Router } from 'express';
import pool from '../db.js';
import { v4 as uuidv4 } from 'uuid';
const router = Router();
// Get all quotations
router.get('/', async (req, res) => {
    try {
        const [rows] = await pool.query(`
      SELECT 
        q.id, 
        q.customer_id, 
        q.project_id, 
        q.quotation_number, 
        q.date, 
        q.expiry_date as valid_until, 
        q.total_amount as sub_total, 
        q.vat_amount, 
        q.grand_total, 
        q.status, 
        q.terms as notes, 
        q.items,
        c.name as customer_name,
        c.address as customer_address,
        c.email as customer_email,
        c.phone as customer_phone,
        c.vat_number as customer_vat_no,
        c.company as customer_company
      FROM quotations q 
      LEFT JOIN customers c ON q.customer_id = c.id 
      ORDER BY q.date DESC
    `);
        res.json(rows);
    }
    catch (error) {
        console.error(error);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});
// Create quotation
router.post('/', async (req, res) => {
    const { quotation_number, customer_id, date, valid_until, sub_total, vat_amount, grand_total, status, notes, items } = req.body;
    const id = uuidv4();
    try {
        await pool.query('INSERT INTO quotations (id, quotation_number, customer_id, date, expiry_date, total_amount, vat_amount, grand_total, status, terms, items) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [id, quotation_number, customer_id, date, valid_until, sub_total, vat_amount, grand_total, status, notes, JSON.stringify(items)]);
        res.status(201).json({ id, quotation_number, customer_id, date, valid_until, sub_total, vat_amount, grand_total, status, notes, items });
    }
    catch (error) {
        console.error(error);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});
// Delete quotation
router.delete('/:id', async (req, res) => {
    const { id } = req.params;
    try {
        await pool.query('DELETE FROM quotations WHERE id = ?', [id]);
        res.json({ message: 'Quotation deleted successfully' });
    }
    catch (error) {
        console.error(error);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});
export default router;
//# sourceMappingURL=quotations.js.map