import stocktransaction from "../models/stocktransaction.js";
import products from "../models/products.js";

export const getReports = async (req, res) => {
  try {
    const today = new Date();
    const daily = new Date();
    daily.setDate(today.getDate() - 1);
    const weekly = new Date();
    weekly.setDate(today.getDate() - 7);
    const monthly = new Date();
    monthly.setMonth(today.getMonth() - 1);
    const dailyStockIn = await stocktransaction.find({
      TransactionType: "IN",
      TransactionDate: { $gte: daily }
    });
    const dailyStockOut = await stocktransaction.find({
      TransactionType: "OUT",
      TransactionDate: { $gte: daily }
    });
    const weeklyStockIn = await stocktransaction.find({
      TransactionType: "IN",
      TransactionDate: { $gte: weekly }
    });
    const weeklyStockOut = await stocktransaction.find({
      TransactionType: "OUT",
      TransactionDate: { $gte: weekly }
    });
    const monthlyStockIn = await stocktransaction.find({
      TransactionType: "IN",
      TransactionDate: { $gte: monthly }
    });
    const monthlyStockOut = await stocktransaction.find({
      TransactionType: "OUT",
      TransactionDate: { $gte: monthly }
    });
    const availableStock = await products.find();
    res.status(200).json({
      dailyStockIn,
      dailyStockOut,

      weeklyStockIn,
      weeklyStockOut,

      monthlyStockIn,
      monthlyStockOut,

      availableStock

    });

  } catch (error) {

    res.status(500).json({
      message: "internal server error",
      error: error.message
    });

  }
};