# Development Module - Development workflows
# ==========================================

# Show detailed development help
@dev-help:
    echo "🔧 DEVELOPMENT MODULE HELP"
    echo "=========================="
    echo ""
    echo "This module provides development workflows including:"
    echo "• Quick development builds"
    echo "• Testing utilities and validation"
    echo "• Performance benchmarking"
    echo "• Development environment management"
    echo ""
    echo "RECIPES:"
    echo "  dev-build         Quick development build (minimal)"
    echo "  test-build        Test build system components"
    echo "  test-dist         Test generated distributions"
    echo "  validate-all      Validate all system components"
    echo "  benchmark         Run comprehensive benchmarks"
    echo "  dev-setup         Setup development environment"
    echo ""
    echo "TESTING:"
    echo "  test-unit         Run unit tests"
    echo "  test-integration  Run integration tests"
    echo "  test-e2e          Run end-to-end tests"
    echo ""
    echo "EXAMPLES:"
    echo "  just dev-build           # Quick build for testing"
    echo "  just test-dist           # Test generated packages"
    echo "  just benchmark           # Performance testing"

# Quick development build (minimal variant)
@dev-build:
    echo "⚡ Quick development build..."
    {{nu}} {{tools_dir}}/distribution/generate-distribution.nu quick \
        --platform linux \
        --variant minimal \
        --output-dir {{dist_dir}}
    echo "✅ Development build completed"

# Test build system components
@test-build:
    echo "🧪 Testing build system..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target {{rust_target}} \
        --release \
        --output-dir {{dist_dir}}/test \
        --verbose
    echo "✅ Build system test completed"

# Test generated distributions
@test-dist:
    #!/usr/bin/env bash
    echo "🧪 Testing distributions..."
    VERBOSE_FLAG=""
    if [ "{{verbose}}" = "true" ]; then
        VERBOSE_FLAG="--verbose"
    fi
    PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]')
    # Check if distributions exist before testing
    if [ -d "{{dist_dir}}" ] && [ "$(ls -A {{dist_dir}} 2>/dev/null)" ]; then
        {{nu}} {{tools_dir}}/build/test-distribution.nu \
            --dist-dir {{dist_dir}} \
            --test-types basic \
            --platform "$PLATFORM" \
            --cleanup \
            $VERBOSE_FLAG 2>/dev/null || true
    else
        echo "ℹ️  No distributions found (skipped)"
    fi
    echo "✅ Distribution testing completed"

# Validate all system components
@validate-all:
    echo "🔍 Validating all components..."
    {{nu}} {{tools_dir}}/build/validate-kcl.nu --verbose={{verbose}}
    {{nu}} {{tools_dir}}/package/validate-package.nu {{dist_dir}} --validation-type complete
    echo "✅ Component validation completed"

# Run comprehensive benchmarks
@benchmark:
    echo "⏱️  Running build benchmarks..."
    echo "Starting benchmark at: $(date)"
    time just build-platform BUILD_MODE=release
    echo "Build benchmark completed at: $(date)"

# Setup development environment
@dev-setup:
    echo "🛠️  Setting up development environment..."
    just check-tools
    just create-dirs
    echo "Installing development dependencies..."
    {{cargo}} install --locked cargo-watch cargo-audit
    echo "✅ Development environment setup completed"

# Watch for changes and rebuild (development mode)
@dev-watch:
    echo "👁️  Watching for changes..."
    {{cargo}} watch -x "build --release" -w provisioning/platform/orchestrator/src

# Run unit tests
@test-unit:
    echo "🧪 Running unit tests..."
    {{cargo}} test --workspace --lib
    echo "✅ Unit tests completed"

# Run integration tests
@test-integration:
    echo "🧪 Running integration tests..."
    {{cargo}} test --workspace --test '*'
    echo "✅ Integration tests completed"

# Run end-to-end tests
@test-e2e:
    echo "🧪 Running end-to-end tests..."
    {{nu}} {{tools_dir}}/test/e2e-tests.nu \
        --dist-dir {{dist_dir}} \
        --cleanup \
        --verbose={{verbose}}
    echo "✅ End-to-end tests completed"

# Run all tests
@test-all: test-unit test-integration test-e2e test-dist
    echo "✅ All tests completed successfully"

# Performance profiling
@profile:
    echo "📊 Running performance profiling..."
    {{cargo}} build --release --bin orchestrator
    echo "Build completed. Run profiler manually on target/release/orchestrator"

# Code quality checks
@quality:
    echo "✨ Running code quality checks..."
    {{cargo}} fmt --check
    {{cargo}} clippy --all-targets --all-features -- -D warnings
    {{cargo}} audit
    echo "✅ Code quality checks completed"

# Development clean (preserve cache)
@dev-clean:
    echo "🧹 Development clean..."
    {{cargo}} clean
    rm -rf {{dist_dir}}/test
    echo "✅ Development artifacts cleaned"

# Show development status
@dev-status:
    echo "🔧 DEVELOPMENT STATUS"
    echo "===================="
    echo "Git branch: $(git rev-parse --abbrev-ref HEAD)"
    echo "Git status:"
    git status --porcelain | head -10
    echo ""
    echo "Rust toolchain:"
    {{cargo}} --version
    rustc --version
    echo ""
    echo "Build targets:"
    rustup target list --installed
    echo ""
    echo "Development directories:"
    ls -la {{dist_dir}} 2>/dev/null || echo "  No dist directory"

# Interactive development shell
@dev-shell:
    echo "🐚 Starting development shell..."
    {{nu}} -e "use provisioning/core/nulib/main.nu *; print 'Provisioning development environment loaded'"
