import User from "../models/User.js";
import Stat from "../models/Stat.js"
import Transaction from "../models/Transaction.js"

export const getUser = async(req, res) => {
    try {
        const { id } = req.params;
        const user = await User.findById(id);
        res.status(200).json(user);


    } catch (error) {
        res.status(404).json({message: error.message})
    }
};

export const getDashboardStats = async(req, res) => {
    try {
        const currerntMonth = "November";
        const currentYear = 2021;
        const currenrtDay = "2021-04-15";
        const transactions = await Transaction.find().limit(50).sort({createdOn: -1});

        const stat = await Stat.find({year: currentYear});
        const {totalCustomers, yearlyTotalSoldUnits, yearlySalesTotal, monthlyData, salesByCategory} = stat[0]

        const thisMonthStats = stat[0].monthlyData.find(({month}) => {
            return month === currerntMonth;
        });
        const todayStats = stat[0].dailyData.find(({date}) => {
            return date === currenrtDay;
        });

        res.status(200).json({totalCustomers, yearlyTotalSoldUnits, yearlySalesTotal, monthlyData, salesByCategory, thisMonthStats, todayStats, transactions})

    } catch (error) {
        res.status(404).json({message: error.message})
    }
}