// Pagination Middleware with Date Filters
const pagination = (req, res, next) => {
  let {
    id,
    page = 1,
    limit = 10,
    start_date,
    end_date,
    tested_by,
    requested_by,
    item_name,
    item_class,
    pt_FName,
    pt_LName,
    q,
    payment_method,
    payer_type,
  } = req.query;

  page = parseInt(page) || 1;
  page = Math.max(1, page);

  limit = parseInt(limit) || 5;
  limit = Math.min(Math.max(1, limit), 50); // Prevent server overload

  req.pagination = {
    id: id || null,
    limit,
    offset: (page - 1) * limit,
    page,
    start_date: start_date || null,
    end_date: end_date || null,
    tested_by: tested_by || null,
    requested_by: requested_by || null,
    q: q || null,
    payment_method: payment_method || null,
    payer_type: payer_type || null,
    item_class,
    item_name,
    pt_FName,
    pt_LName,
  };

  next();
};
module.exports = pagination;
