#!/bin/bash

# Load environment variables
if [ ! -f ../.env ]; then
    echo "Error: .env file not found!"
    exit 1
fi

source ../.env

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# Function to check if a command was successful
check_status() {
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✓ $1${NC}"
        return 0
    else
        echo -e "${RED}✗ $1${NC}"
        return 1
    fi
}

# Function to show usage
show_usage() {
    echo "Usage: $0 [options]"
    echo ""
    echo "Options:"
    echo "  -a, --all         Clean everything (logs, backups, temp files)"
    echo "  -l, --logs        Clean only log files"
    echo "  -b, --backups     Clean only old backups"
    echo "  -t, --temp        Clean only temporary files"
    echo "  -d, --days N      Clean files older than N days (default: 30)"
    echo "  -f, --force       Skip all confirmations"
    echo "  -h, --help        Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 -a             # Clean everything with confirmation"
    echo "  $0 -l -d 7        # Clean logs older than 7 days"
    echo "  $0 -b -f          # Force clean all old backups"
    echo "  $0 -t             # Clean temporary files"
}

# Default values
CLEAN_LOGS=false
CLEAN_BACKUPS=false
CLEAN_TEMP=false
DAYS=30
FORCE=false

# Parse command line options
while [[ $# -gt 0 ]]; do
    case "$1" in
        -a|--all)
            CLEAN_LOGS=true
            CLEAN_BACKUPS=true
            CLEAN_TEMP=true
            shift
            ;;
        -l|--logs)
            CLEAN_LOGS=true
            shift
            ;;
        -b|--backups)
            CLEAN_BACKUPS=true
            shift
            ;;
        -t|--temp)
            CLEAN_TEMP=true
            shift
            ;;
        -d|--days)
            DAYS="$2"
            shift 2
            ;;
        -f|--force)
            FORCE=true
            shift
            ;;
        -h|--help)
            show_usage
            exit 0
            ;;
        *)
            echo "Error: Unknown option '$1'"
            show_usage
            exit 1
            ;;
    esac
done

# If no options specified, show usage
if [ "$CLEAN_LOGS" = false ] && [ "$CLEAN_BACKUPS" = false ] && [ "$CLEAN_TEMP" = false ]; then
    show_usage
    exit 1
fi

# Confirm cleanup if not forced
if [ "$FORCE" = false ]; then
    echo -e "${YELLOW}Warning: This will clean up your development environment.${NC}"
    echo "The following will be cleaned:"
    [ "$CLEAN_LOGS" = true ] && echo "- Log files older than $DAYS days"
    [ "$CLEAN_BACKUPS" = true ] && echo "- Backup files older than $DAYS days"
    [ "$CLEAN_TEMP" = true ] && echo "- Temporary files"
    read -p "Continue? [y/N] " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Operation cancelled."
        exit 1
    fi
fi

echo -e "${BLUE}Starting environment cleanup...${NC}"

# Clean log files
if [ "$CLEAN_LOGS" = true ]; then
    echo "Cleaning log files..."
    
    # Clean WordPress logs
    docker-compose exec wordpress find /var/www/html/wp-content -name "*.log" -type f -mtime +$DAYS -delete
    check_status "WordPress logs cleanup"
    
    # Clean MySQL logs
    docker-compose exec db find /var/log/mysql -name "*.log.*" -type f -mtime +$DAYS -delete
    check_status "MySQL logs cleanup"
fi

# Clean backup files
if [ "$CLEAN_BACKUPS" = true ]; then
    echo "Cleaning old backups..."
    
    # Clean database backups
    find ../backups -name "*.sql" -type f -mtime +$DAYS -delete
    check_status "Database backups cleanup"
    
    # Clean WordPress backups
    find ../backups -name "*.tar.gz" -type f -mtime +$DAYS -delete
    check_status "WordPress backups cleanup"
fi

# Clean temporary files
if [ "$CLEAN_TEMP" = true ]; then
    echo "Cleaning temporary files..."
    
    # Clean WordPress temp files
    docker-compose exec wordpress find /var/www/html/wp-content/uploads/tmp -type f -delete
    check_status "WordPress temp files cleanup"
    
    # Clean local temp files
    find ../tmp -type f -delete 2>/dev/null || true
    check_status "Local temp files cleanup"
fi

echo -e "${GREEN}Environment cleanup completed!${NC}"

# Show disk space saved
echo -e "\n${BLUE}Disk space summary:${NC}"
echo "WordPress volume:"
docker-compose exec wordpress df -h /var/www/html
echo -e "\nMySQL volume:"
docker-compose exec db df -h /var/lib/mysql 