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

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

# =============================================================================
# SMART BUILD FEATURE COMMANDS
# =============================================================================

# Build with intelligent caching (L1/L2/L3 cache layers)
build-smart:
    @echo "⚡ Building with smart caching..."
    cargo run --bin smart-build -- build --cached --optimization aggressive

# Build with cache statistics
build-cached:
    @echo "📊 Building with cache monitoring..."
    cargo run --bin smart-build -- build --cached --stats --output build-stats.json

# Clean build cache selectively
cache-clean level="l1":
    @echo "🧹 Cleaning {{level}} cache layer..."
    cargo run --bin smart-build -- cache clean --level {{level}}

# Clean all cache layers
cache-clean-all:
    @echo "🧹 Cleaning all cache layers..."
    cargo run --bin smart-build -- cache clean --all --force

# Show detailed cache statistics  
cache-stats:
    @echo "📊 Smart build cache statistics:"
    cargo run --bin smart-build -- cache stats --detailed --format table

# Optimize cache for better performance
cache-optimize:
    @echo "⚡ Optimizing build cache..."
    cargo run --bin smart-build -- cache optimize --aggressive --compress

# Validate cache integrity
cache-validate:
    @echo "🔍 Validating cache integrity..."
    cargo run --bin smart-build -- cache validate --repair --verbose

# Show cache usage breakdown
cache-usage:
    @echo "💾 Cache usage breakdown:"
    cargo run --bin smart-build -- cache usage --breakdown --size-limit 1GB

# Export cache configuration
cache-export path="cache-config.json":
    @echo "📤 Exporting cache configuration to {{path}}..."
    cargo run --bin smart-build -- cache export --config --output {{path}}

# Import cache configuration
cache-import path:
    @echo "📥 Importing cache configuration from {{path}}..."
    cargo run --bin smart-build -- cache import --config --file {{path}} --validate

# =============================================================================
# INCREMENTAL BUILD COMMANDS
# =============================================================================

# Incremental build with dependency tracking
build-incremental:
    @echo "🔄 Running incremental build..."
    cargo run --bin smart-build -- build --incremental --track-deps --parallel

# Build only changed components
build-changed:
    @echo "🎯 Building only changed components..."
    cargo run --bin smart-build -- build --changed-only --since HEAD~1

# Rebuild specific component with cache
build-component component:
    @echo "🔨 Building component {{component}} with cache..."
    cargo run --bin smart-build -- build --component {{component}} --cached

# Build with dependency graph analysis
build-analyze:
    @echo "📊 Building with dependency analysis..."
    cargo run --bin smart-build -- build --analyze-deps --output build-analysis.json --visualize

# Build performance profiling
build-profile:
    @echo "⏱️ Building with performance profiling..."
    cargo run --bin smart-build -- build --profile --output build-profile.json --flame-graph

# =============================================================================
# SMART BUILD OPTIMIZATION
# =============================================================================

# Optimize build pipeline
optimize-pipeline:
    @echo "⚡ Optimizing build pipeline..."
    cargo run --bin smart-build -- optimize pipeline --auto-tune --save-config

# Benchmark build performance
benchmark-build iterations="5":
    @echo "🏁 Benchmarking build performance ({{iterations}} iterations)..."
    cargo run --bin smart-build -- benchmark --iterations {{iterations}} --compare-baseline

# Tune cache parameters
tune-cache:
    @echo "🎛️ Auto-tuning cache parameters..."
    cargo run --bin smart-build -- tune cache --auto --save --test-build

# Analyze build bottlenecks
analyze-bottlenecks:
    @echo "🔍 Analyzing build bottlenecks..."
    cargo run --bin smart-build -- analyze bottlenecks --trace --suggestions

# Optimize for CI/CD
optimize-ci:
    @echo "🏭 Optimizing for CI/CD environments..."
    cargo run --bin smart-build -- optimize ci --docker --parallel --cache-remote

# =============================================================================
# CACHE MANAGEMENT WORKFLOWS
# =============================================================================

# Cache maintenance routine
cache-maintain:
    @echo "🔧 Running cache maintenance..."
    #!/usr/bin/env bash
    set -euo pipefail
    
    echo "1. Validating cache integrity..."
    just cache-validate
    
    echo "2. Optimizing cache..."
    just cache-optimize
    
    echo "3. Cleaning old entries..."
    cargo run --bin smart-build -- cache clean --expired --older-than 7d
    
    echo "4. Updating cache statistics..."
    just cache-stats
    
    echo "✅ Cache maintenance completed"

# Cache health check
cache-health:
    @echo "🏥 Smart build cache health check..."
    cargo run --bin smart-build -- cache health --detailed --repair-suggestions

# Setup cache for new project
cache-setup:
    @echo "🚀 Setting up smart build cache..."
    cargo run --bin smart-build -- cache setup --auto-configure --optimal-size

# Cache backup and restore
cache-backup:
    @echo "💾 Backing up smart build cache..."
    mkdir -p backups/smart-build
    cargo run --bin smart-build -- cache backup --output backups/smart-build/cache_$(date +%Y%m%d_%H%M%S).tar.gz

cache-restore backup_file:
    @echo "📂 Restoring smart build cache from {{backup_file}}..."
    cargo run --bin smart-build -- cache restore --file {{backup_file}} --validate

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

# Smart development build (fastest for development)
dev-build:
    @echo "🚀 Smart development build..."
    cargo run --bin smart-build -- build --dev --hot-reload --incremental --fast

# Production build with maximum optimization
prod-build:
    @echo "🏭 Smart production build..."
    cargo run --bin smart-build -- build --prod --optimize-size --optimize-speed --strip

# Test build with cache warming
test-build:
    @echo "🧪 Test build with cache warming..."
    cargo run --bin smart-build -- build --test --warm-cache --parallel
    cargo test

# Clean development workflow
clean-dev:
    @echo "🧹 Clean development reset..."
    just cache-clean l1
    just dev-build
    @echo "✅ Clean development environment ready"

# =============================================================================
# MONITORING AND REPORTING
# =============================================================================

# Generate build report
build-report:
    @echo "📊 Generating smart build report..."
    cargo run --bin smart-build -- report --comprehensive --output reports/build_$(date +%Y%m%d).html

# Monitor build performance
monitor-builds duration="3600":
    @echo "👀 Monitoring build performance for {{duration}} seconds..."
    cargo run --bin smart-build -- monitor --duration {{duration}} --real-time --alert-threshold 120s

# Compare build performance
compare-builds baseline="main":
    @echo "📈 Comparing build performance against {{baseline}}..."
    cargo run --bin smart-build -- compare --baseline {{baseline}} --metrics --visualization

# Smart build dashboard
build-dashboard:
    @echo "📋 Starting smart build dashboard..."
    cargo run --bin smart-build -- dashboard --bind 0.0.0.0:3002 --real-time

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

# =============================================================================
# INTEGRATION AND TESTING
# =============================================================================

# Test smart build functionality
test-smart-build:
    @echo "🧪 Testing smart build functionality..."
    cargo test --package smart-build --all-features
    cargo run --bin smart-build -- test integration --verbose

# Validate smart build setup
validate-setup:
    @echo "✅ Validating smart build setup..."
    cargo run --bin smart-build -- validate setup --environment --dependencies --cache-config

# Integration test with other features
test-integration:
    @echo "🔗 Testing smart build integration..."
    @if command -v npm >/dev/null 2>&1; then \
        if [ -f "e2e/smart-build.spec.js" ]; then \
            npm run test:e2e:smart-build; \
        fi; \
    fi

# Stress test build system
stress-test:
    @echo "💪 Running smart build stress test..."
    cargo run --bin smart-build -- stress-test --duration 300s --concurrent-builds 4 --memory-limit 2GB

# =============================================================================
# CONFIGURATION MANAGEMENT
# =============================================================================

# Show smart build configuration
show-config:
    @echo "⚙️ Smart Build Configuration:"
    cargo run --bin smart-build -- config show --detailed --effective

# Update configuration
update-config key value:
    @echo "🔧 Updating smart build config: {{key}} = {{value}}"
    cargo run --bin smart-build -- config set {{key}} {{value}} --validate

# Reset to default configuration
reset-config:
    @echo "🔄 Resetting smart build configuration to defaults..."
    cargo run --bin smart-build -- config reset --backup --confirm

# Export configuration template
export-config-template:
    @echo "📋 Exporting smart build configuration template..."
    cargo run --bin smart-build -- config template --output config/smart-build-template.toml

# =============================================================================
# STATUS AND INFORMATION
# =============================================================================

# Smart build feature status
smart-build-status:
    @echo "⚡ Smart Build Feature Status:"
    @echo "  Version: $(cargo run --bin smart-build -- version)"
    @echo "  Cache Size: $(cargo run --bin smart-build -- cache stats --json | jq -r '.total_size')"
    @echo "  Hit Rate: $(cargo run --bin smart-build -- cache stats --json | jq -r '.hit_rate')%"
    @echo "  Last Build: $(cargo run --bin smart-build -- status --json | jq -r '.last_build')"
    @echo "  Build Time Saved: $(cargo run --bin smart-build -- status --json | jq -r '.time_saved')"

# Smart build documentation
smart-build-docs:
    @echo "📚 Smart Build feature documentation:"
    @echo "  - Configuration: config/features/smart-build/"
    @echo "  - Cache Location: $(cargo run --bin smart-build -- config get cache_dir)"
    @echo "  - Dashboard: http://localhost:3002"
    @echo "  - Logs: logs/smart-build-*.log"
    @echo "  - Reports: reports/build_*.html"

# =============================================================================
# LOCAL CUSTOMIZATION NOTES
# =============================================================================
# 
# This is a feature-layer justfile that gets imported when smart-build feature
# is enabled. To customize smart build commands locally:
# 
# 1. Create 'config/local/justfile' in your project
# 2. Override any smart-build commands there
# 3. They will take precedence due to layer priority: Local > Feature > Template
#
# Example local override:
# ```
# # Override build-smart with custom optimization
# build-smart:
#     @echo "🎯 Custom smart build with team settings..."
#     cargo run --bin smart-build -- build --cached --team-optimized --notification slack
# ```
# =============================================================================