#!/bin/bash

# GIX DEM Tools Backend - PM2 Manager
# Usage: ./pm2-manager.sh [start|stop|restart|status|logs|reload]

APP_NAME="gix-demtools-backend"
APP_DIR="/var/www/html/gix-demtools/backend"
CONFIG_FILE="ecosystem.config.cjs"
PROD_CONFIG_FILE="ecosystem.prod.config.cjs"

cd "$APP_DIR" || exit 1

case "$1" in
    start)
        echo "🚀 Starting $APP_NAME..."
        pm2 start "$CONFIG_FILE"
        pm2 save
        echo "✓ Application started"
        ;;
    
    start-prod)
        echo "🚀 Starting $APP_NAME in production mode..."
        pm2 start "$PROD_CONFIG_FILE"
        pm2 save
        echo "✓ Application started in production mode"
        ;;
    
    stop)
        echo "⏹️  Stopping $APP_NAME..."
        pm2 stop "$APP_NAME"
        echo "✓ Application stopped"
        ;;
    
    restart)
        echo "🔄 Restarting $APP_NAME..."
        pm2 restart "$APP_NAME"
        echo "✓ Application restarted"
        ;;
    
    reload)
        echo "🔄 Reloading $APP_NAME (zero-downtime)..."
        pm2 reload "$APP_NAME"
        echo "✓ Application reloaded"
        ;;
    
    delete)
        echo "🗑️  Removing $APP_NAME from PM2..."
        pm2 delete "$APP_NAME"
        pm2 save
        echo "✓ Application removed"
        ;;
    
    status)
        echo "📊 Status of $APP_NAME:"
        pm2 list | grep "$APP_NAME"
        echo ""
        pm2 info "$APP_NAME"
        ;;
    
    logs)
        echo "📄 Showing logs for $APP_NAME..."
        pm2 logs "$APP_NAME"
        ;;
    
    logs-error)
        echo "❌ Showing error logs for $APP_NAME..."
        pm2 logs "$APP_NAME" --err
        ;;
    
    logs-out)
        echo "📝 Showing output logs for $APP_NAME..."
        pm2 logs "$APP_NAME" --out
        ;;
    
    flush-logs)
        echo "🗑️  Flushing logs for $APP_NAME..."
        pm2 flush "$APP_NAME"
        echo "✓ Logs flushed"
        ;;
    
    monit)
        echo "📊 Opening PM2 monitoring dashboard..."
        pm2 monit
        ;;
    
    env)
        echo "🔧 Environment variables for $APP_NAME:"
        pm2 env "$APP_NAME"
        ;;
    
    install-deps)
        echo "📦 Installing dependencies..."
        npm install
        echo "✓ Dependencies installed"
        ;;
    
    update)
        echo "🔄 Updating application..."
        git pull
        npm install
        npm run migrate
        pm2 reload "$APP_NAME"
        echo "✓ Application updated"
        ;;
    
    setup-startup)
        echo "⚙️  Setting up PM2 startup script..."
        pm2 startup
        echo ""
        echo "ℹ️  Execute the command shown above, then run:"
        echo "   pm2 save"
        ;;
    
    health)
        echo "🏥 Health check for $APP_NAME..."
        response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3003/health)
        if [ "$response" = "200" ]; then
            echo "✓ Application is healthy (HTTP $response)"
        else
            echo "✗ Application is not responding properly (HTTP $response)"
        fi
        ;;
    
    *)
        echo "GIX DEM Tools Backend - PM2 Manager"
        echo ""
        echo "Usage: $0 [command]"
        echo ""
        echo "Commands:"
        echo "  start          - Start application (development)"
        echo "  start-prod     - Start application (production with 2 instances)"
        echo "  stop           - Stop application"
        echo "  restart        - Restart application"
        echo "  reload         - Reload application (zero-downtime)"
        echo "  delete         - Remove application from PM2"
        echo "  status         - Show application status"
        echo "  logs           - Show application logs (tail -f)"
        echo "  logs-error     - Show error logs only"
        echo "  logs-out       - Show output logs only"
        echo "  flush-logs     - Clear all logs"
        echo "  monit          - Open monitoring dashboard"
        echo "  env            - Show environment variables"
        echo "  install-deps   - Install npm dependencies"
        echo "  update         - Update app (git pull, npm install, migrate, reload)"
        echo "  setup-startup  - Configure PM2 to start on system boot"
        echo "  health         - Check application health"
        echo ""
        exit 1
        ;;
esac
