#!/bin/bash
# Cleanup Old Backups Script
# Enforces retention policy across all backup types

set -e

# Configuration
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
BACKUP_ROOT="$PROJECT_ROOT/backups"
LOG_FILE="$BACKUP_ROOT/cleanup.log"

# Retention periods (in days)
DAILY_RETENTION=7
INCREMENTAL_RETENTION=3
SAFETY_RETENTION=30

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

log() {
    echo "[$(date +"%Y-%m-%d %H:%M:%S")] $1" | tee -a "$LOG_FILE"
}

log "${YELLOW}Starting backup cleanup...${NC}"

# Daily backups - keep last 7
if [ -d "$BACKUP_ROOT/daily" ]; then
    log "Cleaning daily backups (keeping last $DAILY_RETENTION)..."
    cd "$BACKUP_ROOT/daily"
    DELETED=$(ls -t full-backup-*.tar.gz 2>/dev/null | tail -n +$((DAILY_RETENTION + 1)) | wc -l)
    ls -t full-backup-*.tar.gz 2>/dev/null | tail -n +$((DAILY_RETENTION + 1)) | xargs -r rm -f
    log "  Deleted $DELETED daily backups"
fi

# Weekly backups - keep last 4
if [ -d "$BACKUP_ROOT/weekly" ]; then
    log "Cleaning weekly backups (keeping last 4)..."
    cd "$BACKUP_ROOT/weekly"
    DELETED=$(ls -t weekly-backup-*.tar.gz 2>/dev/null | tail -n +5 | wc -l)
    ls -t weekly-backup-*.tar.gz 2>/dev/null | tail -n +5 | xargs -r rm -f
    log "  Deleted $DELETED weekly backups"
fi

# Monthly backups - keep last 3
if [ -d "$BACKUP_ROOT/monthly" ]; then
    log "Cleaning monthly backups (keeping last 3)..."
    cd "$BACKUP_ROOT/monthly"
    DELETED=$(ls -t monthly-backup-*.tar.gz 2>/dev/null | tail -n +4 | wc -l)
    ls -t monthly-backup-*.tar.gz 2>/dev/null | tail -n +4 | xargs -r rm -f
    log "  Deleted $DELETED monthly backups"
fi

# Incremental backups - keep last 3 days
if [ -d "$BACKUP_ROOT/incremental" ]; then
    log "Cleaning incremental backups (keeping last $INCREMENTAL_RETENTION days)..."
    cd "$BACKUP_ROOT/incremental"
    DELETED=$(find . -name "content-*.tar.gz" -type f -mtime +$INCREMENTAL_RETENTION 2>/dev/null | wc -l)
    find . -name "content-*.tar.gz" -type f -mtime +$INCREMENTAL_RETENTION -delete 2>/dev/null || true
    log "  Deleted $DELETED incremental backups"
fi

# Pre-deploy config backups - keep last 10
if [ -d "$BACKUP_ROOT/pre-deploy" ]; then
    log "Cleaning pre-deploy backups (keeping last 10)..."
    cd "$BACKUP_ROOT/pre-deploy"
    DELETED=$(ls -t config-*.tar.gz 2>/dev/null | tail -n +11 | wc -l)
    ls -t config-*.tar.gz 2>/dev/null | tail -n +11 | xargs -r rm -f
    log "  Deleted $DELETED pre-deploy backups"
fi

# Safety backups - keep last 30 days
if [ -d "$BACKUP_ROOT/safety" ]; then
    log "Cleaning safety backups (keeping last $SAFETY_RETENTION days)..."
    cd "$BACKUP_ROOT/safety"
    DELETED=$(find . -name "pre-restore-*.tar.gz" -type f -mtime +$SAFETY_RETENTION 2>/dev/null | wc -l)
    find . -name "pre-restore-*.tar.gz" -type f -mtime +$SAFETY_RETENTION -delete 2>/dev/null || true
    log "  Deleted $DELETED safety backups"
fi

# Report storage usage
log "Current backup storage usage:"
du -sh "$BACKUP_ROOT"/* 2>/dev/null | tee -a "$LOG_FILE" || true

# Check for large backups (>500MB)
log "Checking for large backups (>500MB)..."
find "$BACKUP_ROOT" -name "*.tar.gz" -type f -size +500M -exec ls -lh {} \; 2>/dev/null | tee -a "$LOG_FILE" || true

log "${GREEN}Cleanup completed${NC}"

# Summary
echo ""
echo -e "${GREEN}=== Cleanup Summary ===${NC}"
echo -e "Total backup storage: $(du -sh $BACKUP_ROOT | cut -f1)"
echo -e "Daily backups: $(ls $BACKUP_ROOT/daily/*.tar.gz 2>/dev/null | wc -l)"
echo -e "Weekly backups: $(ls $BACKUP_ROOT/weekly/*.tar.gz 2>/dev/null | wc -l)"
echo -e "Monthly backups: $(ls $BACKUP_ROOT/monthly/*.tar.gz 2>/dev/null | wc -l)"
echo -e "Pre-deploy backups: $(ls $BACKUP_ROOT/pre-deploy/*.tar.gz 2>/dev/null | wc -l)"
