import mongoose from 'mongoose'

export default defineEventHandler(async (event) => {
  const checks = {
    status: 'ok',
    timestamp: new Date().toISOString(),
    uptime: process.uptime(),
    mongodb: 'disconnected',
    memory: process.memoryUsage(),
  }

  // Check MongoDB connection
  try {
    if (mongoose.connection.readyState === 1) {
      checks.mongodb = 'connected'
    } else {
      checks.mongodb = 'disconnected'
    }
  } catch (error) {
    checks.mongodb = 'error'
  }

  // Return 503 if any critical service is down
  const isHealthy = checks.mongodb === 'connected'

  if (!isHealthy) {
    setResponseStatus(event, 503)
    checks.status = 'unhealthy'
  }

  return checks
})
