const customerModel = require('../models/customerModel');

// Global Helper to Auto-Update Customer Totals
exports.autoUpdateCustomerTotals = async (sale_id) => {
    const db = require('../db/config');

    try {
        // Fetch customer_id from the sale
        const [[sale]] = await db.query('SELECT customer_id FROM sales WHERE id = ?', [sale_id]);

        if (!sale) {
            console.warn(`No sale found with ID: ${sale_id}`);
            return;
        }

        // Trigger totals update
        await customerModel.updateCustomerTotals(sale.customer_id);

        console.log(`Customer totals updated for Customer ID: ${sale.customer_id}`);
    } catch (err) {
        console.error('Auto-update customer totals failed:', err.message);
    }
};
