#!/bin/bash
# Health check script for Docker container
# Returns exit code 0 if healthy, 1 if unhealthy

# Check if API is responding
if ! wget --spider -q http://localhost:3001/api/v1/health 2>/dev/null; then
  echo "API health endpoint not responding"
  exit 1
fi

# Check database connection and data
USER_COUNT=$(PGPASSWORD=$DATABASE_PASSWORD psql -h "$DATABASE_HOST" -U "$DATABASE_USER" -d "$DATABASE_NAME" -tAc "SELECT COUNT(*) FROM users;" 2>/dev/null || echo "0")

if [ "$USER_COUNT" -eq "0" ]; then
  echo "Database has no users"
  exit 1
fi

echo "Health check passed: API responding, $USER_COUNT users in database"
exit 0
