# Build Module - Native and Cross-Platform Building
# Commands for building plugins natively and for multiple platforms

# 🔨 NATIVE BUILD COMMANDS

# Build all plugins for current platform
[no-cd]
build:
    @echo "🔨 Building all plugins..."
    @{{justfile_directory()}}/scripts/run.sh build_all.nu

# Build all plugins with verbose output
[no-cd]
build-verbose:
    @echo "🔨 Building all plugins (verbose)..."
    @{{justfile_directory()}}/scripts/run.sh build_all.nu --verbose

# Build all plugins in parallel (experimental)
[no-cd]
build-parallel:
    @echo "⚡ Building all plugins in parallel..."
    @{{justfile_directory()}}/scripts/run.sh build_all.nu --parallel

# Build specific plugin
[no-cd]
build-plugin PLUGIN:
    @echo "🔨 Building {{PLUGIN}}..."
    @cd {{PLUGIN}} && cargo build --release

# 🎯 CROSS-COMPILATION COMMANDS

# List available build targets
[no-cd]
build-targets:
    @echo "📋 Available build targets:"
    @{{justfile_directory()}}/scripts/run.sh build_cross.nu --list-targets

# Build for specific target
[no-cd]
build-cross TARGET:
    @echo "🎯 Cross-compiling for {{TARGET}}..."
    @{{justfile_directory()}}/scripts/run.sh build_cross.nu --targets {{TARGET}}

# Build for all supported targets
[no-cd]
build-cross-all:
    @echo "🌍 Cross-compiling for all targets..."
    @{{justfile_directory()}}/scripts/run.sh build_cross.nu --all-targets

# Build for all targets in parallel
[no-cd]
build-cross-parallel:
    @echo "⚡ Cross-compiling for all targets in parallel..."
    @{{justfile_directory()}}/scripts/run.sh build_cross.nu --all-targets --parallel

# Build for specific target with Docker
[no-cd]
build-docker TARGET:
    @echo "🐳 Building {{TARGET}} with Docker..."
    @{{justfile_directory()}}/scripts/run.sh build_cross.nu --targets {{TARGET}} --docker

# Force native compilation for target
[no-cd]
build-native TARGET:
    @echo "🏠 Building {{TARGET}} natively..."
    @{{justfile_directory()}}/scripts/run.sh build_cross.nu --targets {{TARGET}} --native

# 🐳 DOCKER BUILD COMMANDS

# Build Docker cross-compilation image
[no-cd]
build-docker-image:
    @echo "🐳 Building Docker cross-compilation image..."
    @{{justfile_directory()}}/scripts/run.sh build_docker_cross.nu --build-image

# Force rebuild Docker image from scratch
[no-cd]
build-docker-image-fresh:
    @echo "🐳 Rebuilding Docker image from scratch..."
    @{{justfile_directory()}}/scripts/run.sh build_docker_cross.nu --build-image --force-rebuild --no-cache

# Show Docker environment info
[no-cd]
build-docker-info:
    @echo "🐳 Docker environment information:"
    @{{justfile_directory()}}/scripts/run.sh build_docker_cross.nu --info

# Build specific plugin with Docker
build-docker-plugin PLUGIN TARGET:
    @echo "🐳 Building {{PLUGIN}} for {{TARGET}} with Docker..."
    @{{justfile_directory()}}/scripts/run.sh build_docker_cross.nu --plugin {{PLUGIN}} --target {{TARGET}}

# Clean up Docker artifacts
[no-cd]
build-docker-cleanup:
    @echo "🧹 Cleaning up Docker artifacts..."
    @{{justfile_directory()}}/scripts/run.sh build_docker_cross.nu --cleanup

# 🧹 BUILD MAINTENANCE

# Clean all build artifacts
[no-cd]
build-clean:
    @echo "🧹 Cleaning build artifacts..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Cleaning $$plugin..."; \
            cd "$$plugin" && cargo clean && cd ..; \
        fi; \
    done

# Update all dependencies (careful!)
[no-cd]
build-update-deps:
    @echo "⬆️ Updating dependencies (this may break things)..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Updating deps in $$plugin..."; \
            cd "$$plugin" && cargo update && cd ..; \
        fi; \
    done

# 📊 BUILD INFORMATION

# Show build help
[no-cd]
build-help:
    @echo "🔨 Build Module Help"
    @echo "==================="
    @echo ""
    @echo "NATIVE BUILDS:"
    @echo "  build                    - Build all plugins for current platform"
    @echo "  build-verbose            - Build with verbose output"
    @echo "  build-parallel           - Build in parallel (experimental)"
    @echo "  build-plugin PLUGIN      - Build specific plugin"
    @echo ""
    @echo "CROSS-COMPILATION:"
    @echo "  build-targets            - List available targets"
    @echo "  build-cross TARGET       - Build for specific target"
    @echo "  build-cross-all          - Build for all targets"
    @echo "  build-cross-parallel     - Build all targets in parallel"
    @echo "  build-docker TARGET      - Build with Docker"
    @echo "  build-native TARGET      - Force native compilation"
    @echo ""
    @echo "DOCKER:"
    @echo "  build-docker-image       - Build Docker image"
    @echo "  build-docker-image-fresh - Rebuild Docker image"
    @echo "  build-docker-info        - Show Docker info"
    @echo "  build-docker-plugin PLUGIN TARGET - Build plugin with Docker"
    @echo "  build-docker-cleanup     - Clean Docker artifacts"
    @echo ""
    @echo "MAINTENANCE:"
    @echo "  build-clean              - Clean all artifacts"
    @echo "  build-update-deps        - Update dependencies"
    @echo ""
    @echo "EXAMPLES:"
    @echo "  just build-cross linux-amd64"
    @echo "  just build-docker linux-arm64"
    @echo "  just build-plugin nu_plugin_clipboard"
