# Build Module - Core provisioning build operations
# ==================================================

# Show detailed build help
@build-help:
    echo "🏗️  BUILD MODULE HELP"
    echo "===================="
    echo ""
    echo "This module handles all core build operations including:"
    echo "• Platform binary compilation (Rust orchestrator)"
    echo "• Core Nushell library bundling"
    echo "• Nickel schema configuration (KCL deprecated)"
    echo "• Cross-platform builds"
    echo ""
    echo "RECIPES:"
    echo "  build-all         Complete build (platform + core)"
    echo "  build-platform    Build platform binaries for target architecture"
    echo "  build-core        Bundle and validate Nushell libraries"
    echo "  build-cross       Cross-compile for multiple platforms"
    echo "  build-debug       Build with debug information and symbols"
    echo ""
    echo "CONFIGURATION:"
    echo "  rust_target: {{rust_target}}"
    echo "  build_mode:  {{build_mode}}"
    echo "  platforms:   {{platforms}}"
    echo ""
    echo "EXAMPLES:"
    echo "  just build-all                    # Complete build pipeline"
    echo "  just build-platform --verbose     # Verbose platform build"
    echo "  just build-cross                  # Multi-platform build"

# Complete build pipeline - all components
@build-all: create-dirs build-platform build-core
    echo "✅ Complete build pipeline finished"

# Build platform binaries (Rust orchestrator)
@build-platform:
    #!/usr/bin/env bash
    echo "🔨 Building platform binaries..."
    VERBOSE_FLAG=""
    if [ "{{verbose}}" = "true" ]; then
        VERBOSE_FLAG="--verbose"
    fi
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target {{rust_target}} \
        --{{build_mode}} \
        --output-dir {{dist_dir}}/platform \
        $VERBOSE_FLAG
    echo "✅ Platform binaries built successfully"

# Bundle core Nushell libraries
@build-core:
    #!/usr/bin/env bash
    echo "📚 Building core libraries..."
    VERBOSE_FLAG=""
    EXCLUDE_DEV_FLAG="--exclude-dev true"
    if [ "{{verbose}}" = "true" ]; then
        VERBOSE_FLAG="--verbose"
    fi
    {{nu}} {{tools_dir}}/build/bundle-core.nu \
        --output-dir {{dist_dir}}/core \
        --config-dir {{dist_dir}}/config \
        --validate \
        $EXCLUDE_DEV_FLAG \
        $VERBOSE_FLAG
    echo "✅ Core libraries bundled successfully"

# Cross-compile for multiple platforms
@build-cross:
    #!/usr/bin/env bash
    echo "🌐 Cross-compiling for multiple platforms..."
    IFS=',' read -ra PLATFORM_LIST <<< "{{platforms}}"
    for platform in "${PLATFORM_LIST[@]}"; do
        echo "Building for $platform..."
        {{nu}} {{tools_dir}}/build/compile-platform.nu \
            --target "$platform" \
            --{{build_mode}} \
            --output-dir {{dist_dir}}/platform \
            --verbose={{verbose}} || exit 1
    done
    echo "✅ Cross-compilation completed successfully"

# Build with debug information
@build-debug: create-dirs
    echo "🐛 Building with debug information..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target {{rust_target}} \
        --debug \
        --output-dir {{dist_dir}}/platform \
        --verbose=true
    echo "✅ Debug build completed"

# Fast incremental build for development
@build-incremental:
    echo "⚡ Incremental development build..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target {{rust_target}} \
        --dev \
        --output-dir {{dist_dir}}/platform \
        --incremental \
        --verbose={{verbose}}
    echo "✅ Incremental build completed"

# Clean and rebuild everything
@build-clean-all: clean build-all
    echo "✅ Clean rebuild completed"

# Check build system health
@build-check:
    echo "🔍 Checking build system health..."
    {{nu}} {{tools_dir}}/build/check-system.nu \
        --check-tools \
        --check-dependencies \
        --check-config \
        --verbose={{verbose}}
    echo "✅ Build system health check completed"
