#!/bin/bash
# Archipelago Mac Cleanup Script
# Removes unnecessary local development artifacts
# Safe to run - only removes build caches and Docker data

set -e

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║  Archipelago Mac Cleanup                                       ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

# Track space saved
TOTAL_SAVED=0

# Function to calculate and display savings
calc_savings() {
    local path="$1"
    if [ -e "$path" ]; then
        local size=$(du -sk "$path" | cut -f1)
        TOTAL_SAVED=$((TOTAL_SAVED + size))
    fi
}

# 1. Clean Rust build artifacts (1.6 GB)
if [ -d "core/target" ]; then
    echo "🧹 Cleaning Rust build artifacts..."
    calc_savings "core/target"
    rm -rf core/target
    echo "   ✅ Removed core/target/ (~1.6 GB)"
else
    echo "   ✅ core/target/ already clean"
fi

# 2. Clean ISO build artifacts (8.6 GB)
if [ -d "image-recipe/build" ]; then
    echo "🧹 Cleaning ISO build artifacts..."
    calc_savings "image-recipe/build"
    rm -rf image-recipe/build
    echo "   ✅ Removed image-recipe/build/ (~8.6 GB)"
else
    echo "   ✅ image-recipe/build/ already clean"
fi

# 3. Clean old ISOs (keep latest only)
if [ -d "image-recipe/results" ]; then
    ISO_COUNT=$(ls -1 image-recipe/results/*.iso 2>/dev/null | wc -l | tr -d ' ')
    if [ "$ISO_COUNT" -gt 1 ]; then
        echo "🧹 Cleaning old ISO files (keeping latest)..."
        # Keep the most recent ISO, delete others
        cd image-recipe/results
        ls -t *.iso | tail -n +2 | while read iso; do
            calc_savings "$iso"
            echo "   🗑️  Removing $iso"
            rm "$iso"
        done
        cd ../..
        echo "   ✅ Kept latest ISO, removed old ones (~3 GB saved)"
    else
        echo "   ✅ Only one ISO found, keeping it"
    fi
fi

# 4. Show Docker Desktop warning (requires manual removal)
DOCKER_SIZE=$(du -sk ~/Library/Containers/com.docker.docker 2>/dev/null | cut -f1 || echo "0")
if [ "$DOCKER_SIZE" -gt 1000000 ]; then
    DOCKER_GB=$((DOCKER_SIZE / 1024 / 1024))
    echo ""
    echo "⚠️  Docker Desktop Data Found: ~${DOCKER_GB} GB"
    echo "   Location: ~/Library/Containers/com.docker.docker"
    echo ""
    echo "   Since you develop on the remote server, you likely don't need this."
    echo "   To remove Docker Desktop completely:"
    echo "   1. Open Docker Desktop app"
    echo "   2. Troubleshoot → Uninstall"
    echo "   OR manually: rm -rf ~/Library/Containers/com.docker.docker"
    echo ""
fi

# Summary
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║  Cleanup Complete!                                             ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
SAVED_GB=$((TOTAL_SAVED / 1024 / 1024))
echo "💾 Space saved: ~${SAVED_GB} GB"
echo ""
echo "Your Mac now has:"
echo "  ✅ Source code (for editing)"
echo "  ✅ Deployment scripts (for remote dev)"
echo "  ✅ Latest ISO (for flashing)"
echo "  ❌ No build artifacts (rebuild on remote or in CI)"
echo ""
echo "Development workflow:"
echo "  1. Edit code locally"
echo "  2. Deploy: ./scripts/deploy-to-target.sh --live"
echo "  3. Test on: http://192.168.1.228"
echo ""
echo "To rebuild ISO when needed:"
echo "  cd image-recipe && ./build-debian-iso.sh"
echo ""
