#!/bin/bash

set -euo pipefail

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

echo "🧹 CLEANING ALL PROJECTS 🧹"
echo

# Get all project directories
PROJECT_DIRS=$(find "$SCRIPT_DIR" -maxdepth 1 -type d \
    -not -name ".*" \
    -not -path "$SCRIPT_DIR" | sort)

for dir in $PROJECT_DIRS; do
    PROJECT_NAME=$(basename "$dir")
    
    if [ -f "$dir/clean.sh" ]; then
        echo "Cleaning $PROJECT_NAME..."
        cd "$dir"
        ./clean.sh
        echo
    else
        echo "⚠️  No clean.sh found for $PROJECT_NAME, skipping..."
        echo
    fi
done

# Global Docker cleanup
echo "🐳 Global Docker cleanup..."
echo "Removing unused Docker images..."
docker image prune -f

echo "Removing unused Docker containers..."
docker container prune -f

echo "Removing unused Docker networks..."
docker network prune -f

echo "Removing unused Docker volumes..."
docker volume prune -f

echo
echo "✅ All projects cleaned successfully!"