/**
 * Response Utilities
 * Consistent API response formatting
 */

/**
 * Success response
 * @param {Object} res - Express response object
 * @param {any} data - Response data
 * @param {string} message - Optional success message
 * @param {number} statusCode - HTTP status code (default: 200)
 */
export function success(res, data = null, message = null, statusCode = 200) {
  const response = {
    success: true,
  }

  if (message) {
    response.message = message
  }

  if (data !== null) {
    response.data = data
  }

  return res.status(statusCode).json(response)
}

/**
 * Created response (201)
 */
export function created(res, data, message = 'Erfolgreich erstellt') {
  return success(res, data, message, 201)
}

/**
 * No content response (204)
 */
export function noContent(res) {
  return res.status(204).send()
}

/**
 * Paginated response
 */
export function paginated(res, data, pagination) {
  return res.status(200).json({
    success: true,
    data,
    pagination: {
      page: pagination.page || 1,
      limit: pagination.limit || 25,
      total: pagination.total || data.length,
      totalPages: Math.ceil((pagination.total || data.length) / (pagination.limit || 25)),
      hasMore: pagination.hasMore || false,
    },
  })
}

/**
 * Redirect response
 */
export function redirect(res, url, statusCode = 302) {
  return res.redirect(statusCode, url)
}

/**
 * Error response
 */
export function error(res, errorData, statusCode = 400) {
  return res.status(statusCode).json({
    success: false,
    error: errorData,
  })
}

/**
 * Response helpers object
 */
export const respond = {
  success,
  created,
  noContent,
  paginated,
  redirect,
  error,
}

export default respond
