#!/bin/bash
# Cleanup old deployment versions to prevent disk exhaustion
# Deployment version format: quantengine_YYYYMMDD_HHMMSS_COMMIT_HASH_[SEQ]
# Usage: ./cleanup_old_deployments.sh [keep_count]

set -euo pipefail

DEPLOY_BASE="/home/kjh2064/deployments"
KEEP_COUNT="${1:-5}"  # Keep 5 most recent versions by default

echo "=========================================="
echo "🧹 QuantEngine Deployment Cleanup"
echo "=========================================="
echo "Base: $DEPLOY_BASE"
echo "Keeping: $KEEP_COUNT most recent versions"
echo ""

# Get active deployment
ACTIVE_LINK="/home/kjh2064/quantengine_active"
if [ ! -L "$ACTIVE_LINK" ]; then
    echo "❌ Active link not found: $ACTIVE_LINK"
    exit 1
fi

ACTIVE_VERSION=$(readlink -f "$ACTIVE_LINK")
ACTIVE_NAME=$(basename "$ACTIVE_VERSION")

echo "🔒 Active version: $ACTIVE_NAME"
echo ""

# List all quantengine deployments sorted by date (newest first)
echo "📋 Available versions:"
VERSIONS=$(ls -dt "$DEPLOY_BASE"/quantengine_* 2>/dev/null | head -$((KEEP_COUNT + 1)) | sed 's|.*/||' | sort -r)
COUNT=0

TO_DELETE=""
while IFS= read -r version; do
    VERSION_PATH="$DEPLOY_BASE/$version"
    SIZE=$(du -sh "$VERSION_PATH" 2>/dev/null | cut -f1)

    COUNT=$((COUNT + 1))

    # Check if this is active
    if [ "$VERSION_PATH" = "$ACTIVE_VERSION" ]; then
        echo "  ✓ [$COUNT] $version ($SIZE) [ACTIVE]"
    elif [ $COUNT -le $KEEP_COUNT ]; then
        echo "  · [$COUNT] $version ($SIZE)"
    else
        echo "  ✗ [$COUNT] $version ($SIZE) [WILL DELETE]"
        TO_DELETE="$TO_DELETE $VERSION_PATH"
    fi
done <<< "$VERSIONS"

# Also check for TEST versions (cleanup more aggressively)
echo ""
echo "🧪 Cleaning up TEST versions..."
TEST_VERSIONS=$(ls -dt "$DEPLOY_BASE"/quantengine_*_TEST 2>/dev/null | head -1 || true)
if [ -n "$TEST_VERSIONS" ]; then
    for tv in $TEST_VERSIONS; do
        if [ "$tv" != "$(readlink -f "$ACTIVE_LINK")" ]; then
            TEST_SIZE=$(du -sh "$tv" 2>/dev/null | cut -f1)
            echo "  ✗ $(basename "$tv") ($TEST_SIZE) [STAGING]"
            TO_DELETE="$TO_DELETE $tv"
        fi
    done
fi

# Execute deletion if not in dry-run mode
if [ -n "$TO_DELETE" ] && [ "${2:-}" != "dry-run" ]; then
    echo ""
    echo "🔄 Removing old versions..."
    for dir in $TO_DELETE; do
        if [ -d "$dir" ]; then
            DIR_NAME=$(basename "$dir")
            DIR_SIZE=$(du -sh "$dir" | cut -f1)
            rm -rf "$dir"
            echo "  ✓ Deleted $DIR_NAME ($DIR_SIZE)"
        fi
    done

    FREED=$(du -sh "$DEPLOY_BASE" 2>/dev/null | cut -f1 || echo "unknown")
    echo ""
    echo "✅ Cleanup complete. Total size: $FREED"
else
    echo ""
    echo "⏭️  Dry-run mode. Use 'cleanup_old_deployments.sh $KEEP_COUNT' (without dry-run) to delete."
fi

echo ""
echo "💡 Recommendation:"
echo "  - Run weekly via cron: 0 2 * * 0 /home/kjh2064/scripts/cleanup_old_deployments.sh 3"
echo "  - Or after each successful deployment"
