#!/bin/bash

# Hueman Website Backup Script
# This script creates automatic backups of the website

# Configuration
BACKUP_DIR="/Applications/XAMPP/xamppfiles/htdocs/hueman/backups"
SOURCE_DIR="/Applications/XAMPP/xamppfiles/htdocs/hueman"
DATE=$(date +%Y%m%d_%H%M%S)
DAYS_TO_KEEP=7
WEEKS_TO_KEEP=4
MONTHS_TO_KEEP=12

# Create backup directories if they don't exist
mkdir -p "$BACKUP_DIR/daily"
mkdir -p "$BACKUP_DIR/weekly"
mkdir -p "$BACKUP_DIR/monthly"
mkdir -p "$BACKUP_DIR/manual"

# Function to create backup
create_backup() {
    local backup_type=$1
    local backup_file="$BACKUP_DIR/$backup_type/hueman_backup_${backup_type}_${DATE}.tar.gz"
    
    echo "Creating $backup_type backup..."
    
    # Create compressed backup excluding backups directory itself
    tar -czf "$backup_file" \
        --exclude="$BACKUP_DIR" \
        --exclude="preline-main" \
        --exclude="bak" \
        --exclude="node_modules" \
        --exclude=".git" \
        -C "$SOURCE_DIR" \
        . 2>/dev/null
    
    if [ $? -eq 0 ]; then
        echo "✓ $backup_type backup created successfully: $backup_file"
        echo "  Size: $(du -h "$backup_file" | cut -f1)"
        
        # Save backup metadata
        echo "{
  \"date\": \"$DATE\",
  \"type\": \"$backup_type\",
  \"file\": \"$(basename "$backup_file")\",
  \"size\": \"$(du -h "$backup_file" | cut -f1)\",
  \"created\": \"$(date -Iseconds)\"
}" > "$BACKUP_DIR/$backup_type/backup_${backup_type}_${DATE}.json"
    else
        echo "✗ Failed to create $backup_type backup"
        return 1
    fi
}

# Function to clean old backups
cleanup_old_backups() {
    local backup_type=$1
    local keep_count=$2
    
    echo "Cleaning old $backup_type backups (keeping last $keep_count)..."
    
    # Remove old backup files
    cd "$BACKUP_DIR/$backup_type"
    ls -t hueman_backup_${backup_type}_*.tar.gz | tail -n +$((keep_count + 1)) | xargs rm -f 2>/dev/null
    ls -t backup_${backup_type}_*.json | tail -n +$((keep_count + 1)) | xargs rm -f 2>/dev/null
    
    echo "✓ Old $backup_type backups cleaned"
}

# Main backup function
main() {
    local backup_type=$1
    
    case $backup_type in
        daily)
            create_backup "daily"
            cleanup_old_backups "daily" $DAYS_TO_KEEP
            ;;
        weekly)
            create_backup "weekly"
            cleanup_old_backups "weekly" $WEEKS_TO_KEEP
            ;;
        monthly)
            create_backup "monthly"
            cleanup_old_backups "monthly" $MONTHS_TO_KEEP
            ;;
        manual)
            create_backup "manual"
            ;;
        *)
            echo "Usage: $0 {daily|weekly|monthly|manual}"
            exit 1
            ;;
    esac
    
    # Generate backup report
    generate_report
}

# Function to generate backup report
generate_report() {
    local report_file="$BACKUP_DIR/backup_report.json"
    
    echo "Generating backup report..."
    
    echo "{
  \"generated\": \"$(date -Iseconds)\",
  \"daily_backups\": $(ls -1 "$BACKUP_DIR/daily"/*.tar.gz 2>/dev/null | wc -l),
  \"weekly_backups\": $(ls -1 "$BACKUP_DIR/weekly"/*.tar.gz 2>/dev/null | wc -l),
  \"monthly_backups\": $(ls -1 "$BACKUP_DIR/monthly"/*.tar.gz 2>/dev/null | wc -l),
  \"manual_backups\": $(ls -1 "$BACKUP_DIR/manual"/*.tar.gz 2>/dev/null | wc -l),
  \"total_size\": \"$(du -sh "$BACKUP_DIR" | cut -f1)\"
}" > "$report_file"
    
    echo "✓ Backup report generated: $report_file"
}

# Run main function
main "$@"
