#!/bin/bash
# Module 3: All Services

cat << 'EOF'
---

## 3️⃣ ALL SERVICES

### Docker Containers Overview

EOF

RUNNING=$(docker ps -q | wc -l)
STOPPED=$(docker ps -aq -f status=exited | wc -l)
PAUSED=$(docker ps -aq -f status=paused | wc -l)

echo "- **Running:** $RUNNING containers"
echo "- **Stopped:** $STOPPED containers"
echo "- **Paused:** $PAUSED containers"
echo ""
echo "### Running Containers"
echo ""

docker ps --format '{{.Names}}|{{.Status}}|{{.Image}}' | sort | while IFS='|' read -r name status image; do
    if echo "$status" | grep -q "unhealthy"; then
        EMOJI="🔴"
    elif echo "$status" | grep -q "healthy"; then
        EMOJI="🟢"
    else
        EMOJI="🟡"
    fi
    
    RESTARTS=$(docker inspect --format='{{.RestartCount}}' "$name" 2>/dev/null || echo "0")
    CREATED=$(docker inspect --format='{{.Created}}' "$name" 2>/dev/null | cut -d'T' -f1)
    
    cat << SERVICE

#### $EMOJI **$name**

- **Status:** $status
- **Image:** \`$image\`
- **Restarts:** $RESTARTS
- **Created:** $CREATED
- **View logs:** \`docker logs -f --tail 100 $name\`
- **Restart:** \`docker restart $name\`
- **Stop:** \`docker stop $name\`

SERVICE
done

if [[ "$STOPPED" -gt 0 ]]; then
    cat << EOF

### ⚫ Stopped Containers

EOF
    docker ps -a -f status=exited --format '- **{{.Names}}** - Stopped {{.Status}}' 
fi
