# justfile for dagx crate
# High-level development and CI workflows

# =============================================================================
# Configuration
# =============================================================================

# Treat all warnings as errors
rustflags := "-D warnings"

# Code coverage target percentage (fail if below this threshold)
coverage_target := "80"

# Timeout for coverage analysis in seconds
coverage_timeout := "600"

# Benchmark timeout in seconds
bench_timeout := "300"

# =============================================================================

# Default recipe - show available recipes
default:
    @just --list

# =============================================================================
# Primary Development Workflows
# =============================================================================

# Quick development loop: format + lint + fast tests
dev:
    #!/usr/bin/env bash
    set -e
    echo "🔄 Running development checks..."
    cargo fmt
    RUSTFLAGS="{{rustflags}}" cargo clippy --all-targets --all-features &
    RUSTFLAGS="{{rustflags}}" cargo nextest run --lib --all-features &
    wait
    echo "✅ Development checks passed"

# Pre-commit checks: format-check + lint + typecheck
check:
    @echo "Running format check..."
    cargo fmt -- --check
    @echo ""
    @echo "Running clippy..."
    RUSTFLAGS="{{rustflags}}" cargo clippy --all-targets --all-features
    RUSTFLAGS="{{rustflags}}" cargo clippy --all-targets --no-default-features
    @echo ""
    @echo "Verifying compilation..."
    RUSTFLAGS="{{rustflags}}" cargo test --lib --all-features --no-run
    RUSTFLAGS="{{rustflags}}" cargo test --lib --no-default-features --no-run
    @echo ""
    @echo "✅ All checks passed"

# Run all tests
test:
    @echo "Running all tests..."
    RUSTFLAGS="{{rustflags}}" cargo nextest run --lib --all-features
    @echo "✅ All tests passed"

# Before pushing: check + test + coverage
pre-push: check test coverage
    @echo ""
    @echo "✅ Pre-push checks passed - safe to push!"

# Comprehensive pre-release validation
pre-release: check test coverage audit examples bench-check
    #!/usr/bin/env bash
    set -e

    # Verify documentation version matches crate version
    echo ""
    echo "📋 Verifying documentation version..."
    VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
    MAJOR_MINOR=$(echo $VERSION | cut -d. -f1-2)

    if ! grep -q "dagx = \"$MAJOR_MINOR\"" README.md; then
        echo "❌ ERROR: README.md should reference version $MAJOR_MINOR (crate is $VERSION)"
        echo "   Update installation examples in README.md"
        exit 1
    fi
    echo "✅ Documentation version correct: $MAJOR_MINOR"

    # Package verification
    echo ""
    echo "📦 Verifying package..."
    cargo publish --dry-run
    cargo package --list > /tmp/dagx-files.txt
    echo "✅ Package verified"

    echo ""
    echo "✅ All pre-release checks passed!"
    echo "  - Code formatted and linted"
    echo "  - All tests passing"
    echo "  - Code coverage ≥ {{coverage_target}}%"
    echo "  - No security vulnerabilities"
    echo "  - Dependencies verified"
    echo "  - All examples run successfully"
    echo "  - All benchmarks compile"
    echo "  - Documentation version verified"
    echo "  - Package ready for publication"
    echo ""
    echo "📦 Ready to publish! Run: cargo publish"

# =============================================================================
# Quality & Security
# =============================================================================

# Run code coverage analysis
coverage:
    @echo "Running code coverage analysis (target: {{coverage_target}}%)..."
    cargo llvm-cov nextest --lib --html --output-dir coverage --fail-under-lines {{coverage_target}}
    @echo "Coverage report: coverage/html/index.html"

# Mutation testing with cargo-mutants
mutants:
    @echo "Running mutation testing..."
    @echo "⚠️  This will use significant CPU and memory resources!"
    cargo mutants \
        --no-shuffle \
        --test-tool nextest \
        --jobs 3 \
        --all-features \
        -vV

# Security audit + dependency checks
audit:
    @echo "🔒 Running security checks..."
    cargo audit
    @echo "✅ Security checks passed"

# Run Miri for undefined behavior detection
miri:
    @echo "Running Miri tests for UB detection..."
    cargo +nightly miri test --lib

# =============================================================================
# Benchmarks
# =============================================================================

# Verify benchmarks compile
bench-check:
    @echo "Verifying benchmarks compile..."
    cargo bench --no-run
    @echo "✅ All benchmarks compile"

# Run all benchmarks
bench:
    cargo bench

# Run benchmarks and save as baseline
bench-baseline:
    cargo bench -- --save-baseline baseline

# Compare benchmarks against baseline
bench-compare:
    cargo bench -- --baseline baseline

# Run specific benchmark suite
bench-suite suite:
    cargo bench --bench {{suite}}

# =============================================================================
# Fuzzing
# =============================================================================

# Run a single fuzz target for 60 seconds
fuzz target:
    @echo "Fuzzing {{target}} for 60 seconds..."
    cargo +nightly fuzz run {{target}} -- -max_total_time=60

# Run all fuzz targets sequentially for quick smoke test
fuzz-all:
    #!/usr/bin/env bash
    set -e
    echo "🔬 Running all fuzz targets..."
    for target in fuzz_dag_execution fuzz_dependencies fuzz_type_safety fuzz_concurrent; do
        echo ""
        echo "Running $target..."
        cargo +nightly fuzz run $target -- -max_total_time=30 -runs=10000
    done
    echo ""
    echo "✅ All fuzz targets completed"

# Run extended fuzzing session (for deeper testing)
fuzz-extended target:
    @echo "Running extended fuzzing session for {{target}}..."
    @echo "⚠️  This will run for 1 hour. Press Ctrl+C to stop early."
    cargo +nightly fuzz run {{target}} -- -max_total_time=3600

# Build all fuzz targets
fuzz-build:
    @echo "Building fuzz targets..."
    cargo +nightly fuzz build
    @echo "✅ All fuzz targets built"

# Minimize corpus for a fuzz target
fuzz-cmin target:
    @echo "Minimizing corpus for {{target}}..."
    cargo +nightly fuzz cmin {{target}}

# Run property-based tests
proptest:
    @echo "Running property-based tests..."
    cargo nextest run property_tests --all-features
    @echo "✅ Property tests passed"

# =============================================================================
# Documentation & Utilities
# =============================================================================

# Build documentation and open in browser
doc:
    cargo doc --no-deps --all-features --open

# Format code
fmt:
    cargo fmt

# Show project information
info:
    @echo "dagx crate - Type-safe DAG execution framework for async Rust"
    @echo ""
    @echo "Rust: $(rustc --version)"
    @echo "Cargo: $(cargo --version)"
    @echo ""
    @echo "Dependencies:"
    @cargo tree --depth 1

# Run all examples
examples:
    @echo "Running all examples..."
    cargo run --example 01_basic
    cargo run --example 02_fan_out
    cargo run --example 03_fan_in
    cargo run --example 04_parallel_computation
    cargo run --example circuit_breaker
    cargo run --example complex_dag
    cargo run --example conditional_workflow
    cargo run --example custom_types
    cargo run --example data_pipeline
    cargo run --example debug_tracing
    cargo run --example error_handling
    cargo run --example large_dag
    cargo run --example parallelism_proof
    cargo run --example retry_strategies
    cargo run --example timeout
    cargo run --example tracing_example
    @echo "✅ All examples completed successfully"

# Clean build artifacts
clean:
    cargo clean
    rm -rf mutants.out/ mutants.out.old/ mutation_history.json
    rm -rf target/criterion
    rm -rf coverage/
