#!/usr/bin/env bash
# Automated maintenance tasks
# Usage: ./maintenance-automation.sh [daily|weekly|monthly]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

MAINTENANCE_TYPE="${1:-daily}"

# Daily maintenance
daily_maintenance() {
    echo "=== Daily Maintenance ==="
    echo ""
    
    # Health check
    echo "1. Running health check..."
    bash "$SCRIPT_DIR/health-check.sh"
    echo ""
    
    # Generate report
    echo "2. Generating daily report..."
    bash "$SCRIPT_DIR/generate-bridge-report.sh daily"
    echo ""
    
    # Check for alerts
    echo "3. Checking for alerts..."
    bash "$SCRIPT_DIR/automated-monitoring.sh"
    echo ""
    
    echo "✅ Daily maintenance complete"
}

# Weekly maintenance
weekly_maintenance() {
    echo "=== Weekly Maintenance ==="
    echo ""
    
    # Run all tests
    echo "1. Running test suite..."
    bash "$SCRIPT_DIR/test-suite.sh all"
    echo ""
    
    # Generate weekly report
    echo "2. Generating weekly report..."
    bash "$SCRIPT_DIR/generate-bridge-report.sh weekly"
    echo ""
    
    # Review logs
    echo "3. Reviewing logs..."
    find "$PROJECT_ROOT/logs" -name "*.log" -mtime -7 -exec ls -lh {} \;
    echo ""
    
    echo "✅ Weekly maintenance complete"
}

# Monthly maintenance
monthly_maintenance() {
    echo "=== Monthly Maintenance ==="
    echo ""
    
    # Comprehensive review
    echo "1. Comprehensive system review..."
    bash "$SCRIPT_DIR/health-check.sh"
    bash "$SCRIPT_DIR/test-suite.sh all"
    echo ""
    
    # Generate monthly report
    echo "2. Generating monthly report..."
    bash "$SCRIPT_DIR/generate-bridge-report.sh monthly"
    echo ""
    
    # Cleanup old logs
    echo "3. Cleaning up old logs..."
    find "$PROJECT_ROOT/logs" -name "*.log" -mtime +30 -delete
    echo ""
    
    # Review dependencies
    echo "4. Reviewing dependencies..."
    echo "Check for updates: foundry, cast, etc."
    echo ""
    
    echo "✅ Monthly maintenance complete"
}

case "$MAINTENANCE_TYPE" in
    daily) daily_maintenance ;;
    weekly) weekly_maintenance ;;
    monthly) monthly_maintenance ;;
    *)
        echo "Usage: $0 [daily|weekly|monthly]"
        exit 1
        ;;
esac

