#!/bin/bash
# =============================================================================
# Aurora Docker Cleanup Script
# =============================================================================
# Cleans up Docker resources to free disk space.
#
# Usage: ./docker-cleanup.sh           (interactive mode)
#        ./docker-cleanup.sh --full    (automatic full cleanup)
# =============================================================================

set -e

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

echo -e "${YELLOW}🧹 Aurora Docker Cleanup${NC}"
echo "========================"
echo ""

# Verify Docker is running
if ! docker info > /dev/null 2>&1; then
  echo -e "${RED}Error: Docker is not running.${NC}"
  exit 1
fi

# Show current disk usage first
echo "📊 Current Docker disk usage:"
docker system df
echo ""

# Stop running containers for this project
echo "📦 Stopping Aurora containers..."
docker compose down 2>/dev/null || true

# Remove dangling images (untagged images from failed builds)
echo ""
echo "🗑️  Removing dangling images..."
docker image prune -f

# Check for --full flag for aggressive cleanup
if [[ "$1" == "--full" ]]; then
    echo ""
    echo -e "${YELLOW}🔥 Full cleanup mode - removing all unused Docker resources...${NC}"
    
    # Remove all unused images, not just dangling ones
    echo "  → Removing unused images..."
    docker image prune -a -f
    
    # Remove build cache
    echo "  → Removing build cache..."
    docker builder prune -a -f
    
    # Remove unused volumes (except named ones we need)
    echo "  → Removing unused volumes..."
    docker volume prune -f
    
    # Remove unused networks
    echo "  → Removing unused networks..."
    docker network prune -f
    
    # Remove node_modules volumes
    echo "  → Removing node_modules volumes..."
    docker volume rm aurora_app_node_modules aurora_web_node_modules 2>/dev/null || true
    
    echo ""
    echo -e "${GREEN}✅ Full cleanup complete!${NC}"
else
    # Interactive mode
    echo ""
    read -p "🔧 Remove Docker build cache? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        docker builder prune -f
        echo -e "${GREEN}✓${NC} Build cache cleared"
    fi

    echo ""
    read -p "🖼️  Remove ALL unused images (not just dangling)? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        docker image prune -a -f
        echo -e "${GREEN}✓${NC} Unused images removed"
    fi

    echo ""
    read -p "📁 Remove node_modules volumes? (forces fresh install) (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        docker volume rm aurora_app_node_modules aurora_web_node_modules 2>/dev/null || true
        echo -e "${GREEN}✓${NC} Node modules volumes removed"
    fi

    echo ""
    read -p "🧨 Run full system prune (removes ALL unused data)? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        docker system prune -a -f --volumes
        echo -e "${GREEN}✓${NC} Full system prune complete"
    fi

    echo ""
    echo -e "${GREEN}✅ Cleanup complete!${NC}"
fi

echo ""
echo "📊 Docker disk usage after cleanup:"
docker system df
echo ""
echo "💡 Tip: Check container logs with: sudo du -sh /var/lib/docker/containers/*/*.log"
echo "💡 Tip: Truncate logs with: sudo truncate -s 0 /var/lib/docker/containers/*/*.log"
echo ""
echo "Run 'docker compose up --build' to rebuild"
