#!/bin/bash

# Production Monitoring Script
# Usage: ./scripts/monitor.sh [--continuous]

set -e

CONTINUOUS=false
if [ "$1" = "--continuous" ]; then
    CONTINUOUS=true
fi

check_status() {
    echo "🔍 PRODUCTION HEALTH CHECK $(date)"
    echo "==============================================="
    
    # Check running containers
    echo "📦 Container Status:"
    if docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep vitron; then
        echo "✅ Containers running:"
        docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep vitron
    else
        echo "❌ No containers running!"
    fi
    echo
    
    # Check memory usage
    echo "💾 Memory Usage:"
    docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" | head -1
    docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" | grep vitron || echo "No container stats available"
    echo
    
    # Check disk usage
    echo "💽 Disk Usage:"
    df -h / | tail -1 | awk '{print "Root: " $3 "/" $2 " (" $5 " used)"}'
    docker system df | grep -E "(REPOSITORY|Images|Containers)" || true
    echo
    
    # Health check
    echo "🏥 Application Health:"
    for container in $(docker ps --format "{{.Names}}" | grep vitron); do
        if docker exec "$container" wget --no-verbose --tries=1 --spider --timeout=5 http://localhost:3000/ 2>/dev/null; then
            echo "✅ $container: Healthy"
        else
            echo "❌ $container: Unhealthy"
        fi
    done
    echo
    
    # External connectivity
    echo "🌐 External Connectivity:"
    if curl -f -s --max-time 10 https://vitrown.com/ > /dev/null; then
        echo "✅ https://vitrown.com/ - Accessible"
    else
        echo "❌ https://vitrown.com/ - Not accessible"
    fi
    
    if curl -f -s --max-time 10 https://api.prod.vitrown.com/api/health/ > /dev/null 2>&1; then
        echo "✅ https://api.prod.vitrown.com/api/health/ - Accessible"
    else
        echo "⚠️  https://api.prod.vitrown.com/api/health/ - Check API server"
    fi
    echo
    
    # Check Traefik status
    echo "🔀 Traefik Status:"
    if docker ps | grep -q traefik; then
        echo "✅ Traefik proxy running"
    else
        echo "❌ Traefik proxy not running!"
    fi
    echo
    
    # Recent logs (last 10 lines)
    echo "📋 Recent Activity (last 10 lines):"
    docker-compose -f docker-compose.yml logs --tail=10 2>/dev/null || echo "No logs available"
    echo
    
    # Backup status
    echo "💾 Backup Status:"
    BACKUP_COUNT=$(docker images | grep -c "vitron-frontend-backup" || echo "0")
    echo "Available backups: $BACKUP_COUNT"
    if [ "$BACKUP_COUNT" -gt 0 ]; then
        echo "Latest backup:"
        docker images | grep "vitron-frontend-backup" | head -1
    fi
    echo
    
    echo "==============================================="
    echo "✅ Health check completed at $(date)"
    echo
}

# Run check
check_status

# Continuous monitoring if requested
if [ "$CONTINUOUS" = true ]; then
    echo "🔄 Continuous monitoring enabled (every 60 seconds)"
    echo "Press Ctrl+C to stop"
    echo
    
    while true; do
        sleep 60
        check_status
    done
fi