#!/bin/bash
# CompareKit Backup Script
# Creates timestamped backups of your project

# Configuration
PROJECT_NAME="CompareKit"
BACKUP_DIR="../CompareKit-Backups"
DATE=$(date +"%Y%m%d-%H%M%S")
BACKUP_NAME="${PROJECT_NAME}-backup-${DATE}"

echo "╔════════════════════════════════════════╗"
echo "║     CompareKit Backup System           ║"
echo "╚════════════════════════════════════════╝"
echo ""

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

echo "📦 Creating backup: $BACKUP_NAME"
echo ""

# Create backup (exclude node_modules, cache, generated files)
tar -czf "${BACKUP_DIR}/${BACKUP_NAME}.tar.gz" \
  --exclude='node_modules' \
  --exclude='cache/*' \
  --exclude='generated/*' \
  --exclude='tmp/*' \
  --exclude='.git' \
  --exclude='*.log' \
  --exclude='.DS_Store' \
  .

if [ $? -eq 0 ]; then
    echo "✅ Backup created successfully!"
    echo ""
    echo "📁 Location: ${BACKUP_DIR}/${BACKUP_NAME}.tar.gz"
    echo "📊 Size: $(du -h "${BACKUP_DIR}/${BACKUP_NAME}.tar.gz" | cut -f1)"
    echo ""

    # List recent backups
    echo "📋 Recent backups:"
    ls -lht "$BACKUP_DIR" | head -6
    echo ""

    # Cleanup old backups (keep last 10)
    echo "🧹 Cleaning up old backups (keeping last 10)..."
    cd "$BACKUP_DIR"
    ls -t ${PROJECT_NAME}-backup-*.tar.gz | tail -n +11 | xargs -r rm --
    echo ""

    echo "✨ Backup complete!"
else
    echo "❌ Backup failed!"
    exit 1
fi
