const logger = require('./logger')

class ResponseHandle {
  static sendResponse(res, statusCode, status, message, data = null, error = null) {
    res.status(statusCode).json({
      status,
      message,
      data: data || undefined,
      error: error || undefined,
    })
  }

  static success(res, data = null, message = 'Success') {
    this.sendResponse(res, 200, 'success', message, data)
  }

  static created(res, data = null, message = 'Resource created successfully') {
    this.sendResponse(res, 201, 'success', message, data)
  }

  static error(res, statusCode, message, error = null) {
    if (statusCode >= 500) {
      logger.error(`${statusCode} Internal Server Error:`, { message, error })
      this.sendResponse(res, statusCode, 'error', message, null, error)
    } else {
      this.sendResponse(res, statusCode, 'fail', message, null, null)
    }
  }
}

module.exports = ResponseHandle
