/**
 * Unified response format utility
 * All API responses should use this format: { code: 0, message: "", data: {} }
 */

/**
 * Send success response
 * @param {Object} res - Express response object
 * @param {*} data - Response data
 * @param {String} message - Success message
 * @param {Number} statusCode - HTTP status code (default: 200)
 */
const sendSuccess = (res, data = null, message = 'Success', statusCode = 200) => {
  res.status(statusCode).json({
    code: 0,
    message,
    data,
  });
};

/**
 * Send error response
 * @param {Object} res - Express response object
 * @param {String} message - Error message
 * @param {Number} code - Error code
 * @param {Number} statusCode - HTTP status code (default: 400)
 */
const sendError = (res, message = 'Error', code = 400, statusCode = 400) => {
  res.status(statusCode).json({
    code,
    message,
    data: null,
  });
};

/**
 * Send paginated response
 * @param {Object} res - Express response object
 * @param {Array} items - Data items
 * @param {Number} total - Total count
 * @param {Number} page - Current page
 * @param {Number} limit - Items per page
 * @param {String} message - Success message
 */
const sendPaginated = (res, items, total, page, limit, message = 'Success') => {
  res.status(200).json({
    code: 0,
    message,
    data: {
      items,
      pagination: {
        total,
        page,
        limit,
        totalPages: Math.ceil(total / limit),
      },
    },
  });
};

module.exports = {
  sendSuccess,
  sendError,
  sendPaginated,
};
