#!/bin/bash

# Supervisor Queue Worker Diagnostic Script
# Run: bash docs/diagnose-supervisor.sh

echo "=========================================="
echo "Supervisor Queue Worker Diagnostic"
echo "=========================================="
echo ""

# 1. Check PHP
echo "1. Checking PHP..."
PHP_PATH=$(which php)
if [ -z "$PHP_PATH" ]; then
    echo "   ❌ PHP not found in PATH"
    echo "   Try: /usr/bin/php or /usr/local/bin/php"
else
    echo "   ✅ PHP found: $PHP_PATH"
    php -v | head -1
fi
echo ""

# 2. Check Artisan
echo "2. Checking Artisan file..."
ARTISAN_PATH="/var/www/html/kejarkoding_com/artisan"
if [ -f "$ARTISAN_PATH" ]; then
    echo "   ✅ Artisan found: $ARTISAN_PATH"
    ls -la "$ARTISAN_PATH"
else
    echo "   ❌ Artisan NOT found: $ARTISAN_PATH"
    echo "   Please check the path in your supervisor config"
fi
echo ""

# 3. Check Directory
echo "3. Checking project directory..."
PROJECT_DIR="/var/www/html/kejarkoding_com"
if [ -d "$PROJECT_DIR" ]; then
    echo "   ✅ Directory exists: $PROJECT_DIR"
    ls -ld "$PROJECT_DIR"
else
    echo "   ❌ Directory NOT found: $PROJECT_DIR"
fi
echo ""

# 4. Check User
echo "4. Checking user (www-data)..."
if id "www-data" &>/dev/null; then
    echo "   ✅ User www-data exists"
    id www-data
else
    echo "   ❌ User www-data NOT found"
    echo "   Current user: $(whoami)"
fi
echo ""

# 5. Check Permissions
echo "5. Checking permissions..."
if [ -f "$ARTISAN_PATH" ]; then
    PERMS=$(stat -c "%a %U:%G" "$ARTISAN_PATH")
    echo "   Artisan permissions: $PERMS"
    
    if [ -r "$ARTISAN_PATH" ] && [ -x "$ARTISAN_PATH" ]; then
        echo "   ✅ Artisan is readable and executable"
    else
        echo "   ❌ Artisan permission issue"
    fi
fi
echo ""

# 6. Test Command Manually
echo "6. Testing queue:work command..."
if [ -f "$ARTISAN_PATH" ] && [ -n "$PHP_PATH" ]; then
    echo "   Running: $PHP_PATH $ARTISAN_PATH queue:work redis --queue=whatsapp --once"
    sudo -u www-data $PHP_PATH $ARTISAN_PATH queue:work redis --queue=whatsapp --once --timeout=5 2>&1 | head -5
    if [ $? -eq 0 ]; then
        echo "   ✅ Command works!"
    else
        echo "   ❌ Command failed (check error above)"
    fi
else
    echo "   ⚠️  Skipping (PHP or Artisan not found)"
fi
echo ""

# 7. Check Supervisor Config
echo "7. Checking supervisor config files..."
SUPERVISOR_DIR="/etc/supervisor/conf.d"
if [ -d "$SUPERVISOR_DIR" ]; then
    echo "   Config directory: $SUPERVISOR_DIR"
    echo "   Config files:"
    ls -la "$SUPERVISOR_DIR"/*.conf 2>/dev/null | awk '{print "      " $9 " (" $5 " bytes)"}'
    
    echo ""
    echo "   Checking whatsapp-queue.conf:"
    if [ -f "$SUPERVISOR_DIR/whatsapp-queue.conf" ]; then
        echo "   ✅ Config file exists"
        echo "   Content:"
        cat "$SUPERVISOR_DIR/whatsapp-queue.conf" | sed 's/^/      /'
    else
        echo "   ❌ Config file NOT found"
    fi
else
    echo "   ❌ Supervisor config directory not found"
fi
echo ""

# 8. Check Log Directory
echo "8. Checking log directory..."
LOG_DIR="/var/www/html/kejarkoding_com/storage/logs"
if [ -d "$LOG_DIR" ]; then
    echo "   ✅ Log directory exists: $LOG_DIR"
    ls -ld "$LOG_DIR"
else
    echo "   ❌ Log directory NOT found: $LOG_DIR"
    echo "   Creating..."
    sudo mkdir -p "$LOG_DIR"
    sudo chown -R www-data:www-data "$LOG_DIR"
    echo "   ✅ Created"
fi
echo ""

# 9. Check Redis Connection
echo "9. Checking Redis connection..."
if command -v redis-cli &> /dev/null; then
    if redis-cli ping &> /dev/null; then
        echo "   ✅ Redis is running"
    else
        echo "   ❌ Redis is NOT running or not accessible"
    fi
else
    echo "   ⚠️  redis-cli not found (skipping Redis check)"
fi
echo ""

# 10. Check .env
echo "10. Checking .env configuration..."
ENV_FILE="/var/www/html/kejarkoding_com/.env"
if [ -f "$ENV_FILE" ]; then
    echo "   ✅ .env file exists"
    if grep -q "QUEUE_CONNECTION=redis" "$ENV_FILE"; then
        echo "   ✅ QUEUE_CONNECTION=redis is set"
    else
        echo "   ⚠️  QUEUE_CONNECTION not set to redis"
        echo "   Current: $(grep QUEUE_CONNECTION "$ENV_FILE" || echo 'not found')"
    fi
else
    echo "   ❌ .env file NOT found"
fi
echo ""

echo "=========================================="
echo "Diagnostic Complete"
echo "=========================================="
echo ""
echo "Next steps:"
echo "1. Fix any issues marked with ❌"
echo "2. Run: sudo supervisorctl reread"
echo "3. Run: sudo supervisorctl update"
echo "4. Run: sudo supervisorctl start whatsapp-queue:*"
echo "5. Check: sudo supervisorctl status whatsapp-queue:*"
