import reportService from '../services/reportService.js';

export const daily = async (req, res, next) => {
  try {
    const date = req.query.date || new Date().toISOString();
    const result = await reportService.getDailySales(date);
    res.json({ success: true, data: result });
  } catch (error) {
    next(error);
  }
};

export const weekly = async (req, res, next) => {
  try {
    const startDate = req.query.startDate || new Date().toISOString();
    const result = await reportService.getWeeklySales(startDate);
    res.json({ success: true, data: result });
  } catch (error) {
    next(error);
  }
};

export const monthly = async (req, res, next) => {
  try {
    const now = new Date();
    const year = Number(req.query.year) || now.getFullYear();
    const month = Number(req.query.month) || now.getMonth() + 1;
    const result = await reportService.getMonthlySales(year, month);
    res.json({ success: true, data: result });
  } catch (error) {
    next(error);
  }
};

export const bestSelling = async (req, res, next) => {
  try {
    const limit = Number(req.query.limit) || 10;
    const result = await reportService.getBestSelling(limit);
    res.json({ success: true, data: result });
  } catch (error) {
    next(error);
  }
};

export const lowStock = async (req, res, next) => {
  try {
    const result = await reportService.getLowStockReport();
    res.json({ success: true, data: result });
  } catch (error) {
    next(error);
  }
};
