#!/bin/bash
# b0esche.cloud Monitoring Script
# Usage: ./monitor.sh

set -e

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo "=== b0esche.cloud Service Status ==="
echo

# Check critical services
services=("traefik" "go-backend" "go-postgres" "flutter-web" "nextcloud" "nextcloud-db" "collabora")

for service in "${services[@]}"; do
    if docker ps --format "table {{.Names}}" | grep -q "^$service$"; then
        echo -e "${GREEN}✓${NC} $service is running"
    else
        echo -e "${RED}✗${NC} $service is not running"
    fi
done

echo
echo "=== Service Health Checks ==="
echo

# HTTP/HTTPS health checks
echo -n "Flutter Web (www.b0esche.cloud): "
if curl -s --max-time 5 https://www.b0esche.cloud | grep -q "b0esche_cloud"; then
    echo -e "${GREEN}OK${NC}"
else
    echo -e "${RED}FAILED${NC}"
fi

echo -n "Go Backend (go.b0esche.cloud): "
if curl -s --max-time 5 https://go.b0esche.cloud/health | grep -q "ok"; then
    echo -e "${GREEN}OK${NC}"
else
    echo -e "${RED}FAILED${NC}"
fi

echo -n "Nextcloud (storage.b0esche.cloud): "
if curl -s --max-time 5 -I https://storage.b0esche.cloud | grep -q "HTTP/2 200"; then
    echo -e "${GREEN}OK${NC}"
else
    echo -e "${RED}FAILED${NC}"
fi

echo -n "Collabora (of.b0esche.cloud): "
if curl -s --max-time 5 -I https://of.b0esche.cloud | grep -q "HTTP/2"; then
    echo -e "${GREEN}OK${NC}"
else
    echo -e "${YELLOW}DEGRADED${NC}"
fi

echo
echo "=== Resource Usage ==="
echo

# Show container resource usage
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" | grep -E "(traefik|go-backend|flutter-web|nextcloud|collabora)"

echo
echo "=== Recent Error Logs ==="
echo

# Show recent error logs from each service
for service in traefik go-backend nextcloud collabora; do
    errors=$(docker logs "$service" --since=1h 2>&1 | grep -i error | tail -3 | wc -l)
    if [ "$errors" -gt 0 ]; then
        echo -e "${YELLOW}$service:${NC} $errors errors in last hour"
        docker logs "$service" --since=1h 2>&1 | grep -i error | tail -3 | sed 's/^/   /'
    fi
done

echo
echo "=== SSL Certificate Status ==="
echo

# Check certificate expiry for main domains
domains=("www.b0esche.cloud" "go.b0esche.cloud" "storage.b0esche.cloud" "of.b0esche.cloud")

for domain in "${domains[@]}"; do
    expiry=$(echo | openssl s_client -servername "$domain" -connect "$domain:443" 2>/dev/null | openssl x509 -noout -dates 2>/dev/null | grep notAfter | cut -d= -f2)
    if [ -n "$expiry" ]; then
        expiry_epoch=$(date -d "$expiry" +%s)
        current_epoch=$(date +%s)
        days_left=$(( (expiry_epoch - current_epoch) / 86400 ))
        
        if [ "$days_left" -lt 7 ]; then
            echo -e "${RED}$domain: ${NC}Expires in $days_left days"
        elif [ "$days_left" -lt 30 ]; then
            echo -e "${YELLOW}$domain: ${NC}Expires in $days_left days"
        else
            echo -e "${GREEN}$domain: ${NC}Expires in $days_left days"
        fi
    else
        echo -e "${RED}$domain: ${NC}Certificate check failed"
    fi
done

echo
echo "=== Disk Usage ==="
echo

# Show disk usage for critical directories
echo "PostgreSQL data:"
du -sh /opt/go/data/postgres 2>/dev/null || echo "  Not accessible"

echo "Backup directory:"
du -sh /opt/backups 2>/dev/null || echo "  Not found"

echo "Docker volumes:"
docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}"

echo
echo "=== Monitoring Complete ==="