# =============================================================================
# Analytics Feature - Justfile Template
# =============================================================================
# This file demonstrates layered override system for feature-specific commands.
# It will be imported by the main justfile when analytics feature is enabled.
# Layer: Feature > Template > Framework

# Set shell for commands
set shell := ["bash", "-c"]

# =============================================================================
# ANALYTICS FEATURE COMMANDS
# =============================================================================

# Start analytics development server
analytics-dev:
    @echo "📊 Starting analytics development server..."
    RUST_LOG=debug cargo run --bin analytics -- dev --port 3001

# Generate analytics report for specified hours
analytics-report hours="24":
    @echo "📈 Generating analytics report for {{hours}} hours..."
    cargo run --bin analytics -- report --hours {{hours}} --format json --output reports/analytics-{{hours}}h.json

# Run analytics dashboard with refresh interval
analytics-dashboard refresh="30":
    @echo "📋 Starting analytics dashboard (refresh every {{refresh}}s)..."
    cargo run --bin analytics -- dashboard --refresh {{refresh}} --bind 0.0.0.0:3001

# Test analytics functionality
test-analytics:
    @echo "🧪 Testing analytics functionality..."
    cargo test --package analytics --all-features
    @if [ -f "tests/analytics/integration_test.rs" ]; then \
        cargo test --test integration_test analytics; \
    fi

# Collect metrics from running application
analytics-collect duration="60":
    @echo "📊 Collecting metrics for {{duration}} seconds..."
    cargo run --bin analytics -- collect --duration {{duration}} --output metrics/$(date +%Y%m%d_%H%M%S).json

# Start analytics monitoring in background
analytics-monitor:
    @echo "👀 Starting analytics monitoring..."
    cargo run --bin analytics -- monitor --daemon --log-file logs/analytics-monitor.log &
    @echo "Analytics monitor started in background. Check logs/analytics-monitor.log for output."

# Stop analytics monitoring
analytics-stop:
    @echo "🛑 Stopping analytics monitoring..."
    pkill -f "analytics.*monitor" || echo "No analytics monitor running"

# Clean analytics data and logs
analytics-clean:
    @echo "🧹 Cleaning analytics data..."
    rm -rf logs/analytics-*.log
    rm -rf metrics/*.json
    rm -rf reports/analytics-*.json
    @echo "Analytics data cleaned"

# Show analytics configuration
analytics-config:
    @echo "⚙️ Analytics Configuration:"
    @echo "  Enabled: $(cargo run --bin analytics -- config --check-enabled)"
    @echo "  Log Level: $(cargo run --bin analytics -- config --get log_level)"
    @echo "  Storage Path: $(cargo run --bin analytics -- config --get storage_path)"
    @echo "  Retention Days: $(cargo run --bin analytics -- config --get retention_days)"

# Analytics health check
analytics-health:
    @echo "🏥 Analytics health check..."
    cargo run --bin analytics -- health --verbose

# Export analytics data
analytics-export format="json" days="7":
    @echo "📤 Exporting analytics data ({{format}}, last {{days}} days)..."
    mkdir -p exports/analytics
    cargo run --bin analytics -- export --format {{format}} --days {{days}} --output exports/analytics/export_$(date +%Y%m%d).{{format}}

# Import analytics data
analytics-import file:
    @echo "📥 Importing analytics data from {{file}}..."
    cargo run --bin analytics -- import --file {{file}} --validate

# =============================================================================
# ANALYTICS DEVELOPMENT WORKFLOWS
# =============================================================================

# Full analytics development setup
analytics-dev-full:
    @echo "🚀 Starting full analytics development environment..."
    #!/usr/bin/env bash
    set -euo pipefail
    
    # Start analytics server in background
    just analytics-dev &
    ANALYTICS_PID=$!
    
    # Start monitoring in background  
    just analytics-monitor &
    MONITOR_PID=$!
    
    # Start dashboard in background
    just analytics-dashboard &
    DASHBOARD_PID=$!
    
    echo "📊 Analytics services started:"
    echo "  - Development server (PID: $ANALYTICS_PID)"
    echo "  - Monitoring (PID: $MONITOR_PID)" 
    echo "  - Dashboard (PID: $DASHBOARD_PID)"
    echo "🌐 Dashboard available at: http://localhost:3001"
    echo "Press Ctrl+C to stop all services"
    
    # Trap Ctrl+C and kill all background processes
    trap 'kill $ANALYTICS_PID $MONITOR_PID $DASHBOARD_PID 2>/dev/null' INT
    
    # Wait for all background processes
    wait

# Analytics testing workflow
analytics-test-full:
    @echo "🧪 Running comprehensive analytics tests..."
    just test-analytics
    just analytics-health
    @if command -v npm >/dev/null 2>&1; then \
        if [ -f "e2e/analytics.spec.js" ]; then \
            echo "🌐 Running E2E analytics tests..."; \
            npm run test:e2e:analytics; \
        fi; \
    fi

# Analytics performance benchmarks
analytics-benchmark:
    @echo "⚡ Running analytics performance benchmarks..."
    cargo bench --package analytics
    @if [ -f "benches/analytics_benchmark.rs" ]; then \
        cargo run --release --bin analytics -- benchmark --iterations 1000 --output benchmarks/results_$(date +%Y%m%d).json; \
    fi

# =============================================================================
# ANALYTICS MAINTENANCE TASKS
# =============================================================================

# Rotate analytics logs
analytics-rotate-logs:
    @echo "🔄 Rotating analytics logs..."
    @if [ -d "logs" ]; then \
        mkdir -p logs/archive; \
        find logs -name "analytics-*.log" -mtime +7 -exec mv {} logs/archive/ \;; \
        gzip logs/archive/*.log 2>/dev/null || true; \
        echo "Logs rotated to logs/archive/"; \
    fi

# Analytics database maintenance
analytics-db-maintain:
    @echo "🗄️ Analytics database maintenance..."
    cargo run --bin analytics -- db maintenance --vacuum --optimize
    cargo run --bin analytics -- db cleanup --retention-days 30

# Analytics backup
analytics-backup:
    @echo "💾 Creating analytics backup..."
    mkdir -p backups/analytics
    cargo run --bin analytics -- backup --output backups/analytics/backup_$(date +%Y%m%d_%H%M%S).tar.gz
    @echo "Backup created in backups/analytics/"

# Analytics restore
analytics-restore backup_file:
    @echo "📂 Restoring analytics from {{backup_file}}..."
    cargo run --bin analytics -- restore --file {{backup_file}} --confirm

# =============================================================================
# FEATURE INTEGRATION HELPERS
# =============================================================================

# Show analytics integration status
analytics-status:
    @echo "📊 Analytics Feature Status:"
    @echo "  Version: $(cargo run --bin analytics -- version)"
    @echo "  Status: $(cargo run --bin analytics -- status --json | jq -r '.status')"
    @echo "  Uptime: $(cargo run --bin analytics -- status --json | jq -r '.uptime')"
    @echo "  Events processed: $(cargo run --bin analytics -- status --json | jq -r '.events_processed')"

# Analytics feature documentation
analytics-docs:
    @echo "📚 Analytics feature documentation:"
    @echo "  - Configuration: config/features/analytics/"
    @echo "  - API endpoints: /api/analytics/*"
    @echo "  - Dashboard: http://localhost:3001"
    @echo "  - Logs: logs/analytics-*.log"
    @echo "  - Metrics: metrics/*.json"

# =============================================================================
# LOCAL CUSTOMIZATION NOTES
# =============================================================================
# 
# This is a feature-layer justfile that gets imported when analytics feature
# is enabled. To customize analytics commands locally:
# 
# 1. Create 'config/local/justfile' in your project
# 2. Override any analytics commands there
# 3. They will take precedence due to layer priority: Local > Feature > Template
#
# Example local override:
# ```
# # Override analytics report with custom format
# analytics-report hours="24":
#     @echo "🎯 Custom analytics report for {{hours}} hours..."
#     cargo run --bin analytics -- report --hours {{hours}} --format custom --detailed
# ```
# =============================================================================