const { sequelize, Order } = require('./models');

const checkDistribution = async () => {
    try {
        console.log("Checking Order Date Distribution...");

        const orders = await Order.findAll({
            attributes: [
                [sequelize.fn('DATE_FORMAT', sequelize.col('created_at'), '%Y-%m'), 'month'],
                [sequelize.fn('COUNT', sequelize.col('id')), 'count']
            ],
            group: [sequelize.fn('DATE_FORMAT', sequelize.col('created_at'), '%Y-%m')],
            order: [[sequelize.fn('DATE_FORMAT', sequelize.col('created_at'), '%Y-%m'), 'ASC']]
        });

        console.log("Distribution:");
        orders.forEach(o => {
            console.log(`${o.dataValues.month}: ${o.dataValues.count} orders`);
        });

    } catch (error) {
        console.error("Error:", error);
    } finally {
        process.exit();
    }
};

checkDistribution();
