# Platform Module - Platform-specific operations
# ===============================================

# Show detailed platform help
@platform-help:
    echo "🌐 PLATFORM MODULE HELP"
    echo "======================="
    echo ""
    echo "This module handles platform-specific builds including:"
    echo "• Individual platform builds (Linux, macOS, Windows)"
    echo "• Cross-compilation management"
    echo "• Platform-specific testing"
    echo "• Architecture-specific optimizations"
    echo ""
    echo "RECIPES:"
    echo "  linux             Build for Linux only"
    echo "  macos             Build for macOS only"
    echo "  windows           Build for Windows only"
    echo "  all-platforms     Build for all supported platforms"
    echo "  platform-test     Test platform-specific builds"
    echo ""
    echo "SUPPORTED PLATFORMS:"
    echo "  • linux-amd64     Linux 64-bit x86"
    echo "  • macos-amd64     macOS 64-bit Intel"
    echo "  • macos-arm64     macOS Apple Silicon"
    echo "  • windows-amd64   Windows 64-bit x86"
    echo ""
    echo "EXAMPLES:"
    echo "  just linux           # Linux-only build"
    echo "  just all-platforms   # All supported platforms"
    echo "  just platform-test   # Test platform builds"

# Build for Linux only
@linux:
    echo "🐧 Building for Linux..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target linux-amd64 \
        --{{build_mode}} \
        --output-dir {{dist_dir}}/platform/linux \
        --verbose={{verbose}}
    {{nu}} {{tools_dir}}/distribution/generate-distribution.nu \
        --version {{version}} \
        --platforms linux-amd64 \
        --variants {{variants}} \
        --output-dir {{dist_dir}} \
        --verbose={{verbose}}
    echo "✅ Linux build completed"

# Build for macOS only
@macos:
    echo "🍎 Building for macOS..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target macos-amd64 \
        --{{build_mode}} \
        --output-dir {{dist_dir}}/platform/macos \
        --verbose={{verbose}}
    {{nu}} {{tools_dir}}/distribution/generate-distribution.nu \
        --version {{version}} \
        --platforms macos-amd64 \
        --variants {{variants}} \
        --output-dir {{dist_dir}} \
        --verbose={{verbose}}
    echo "✅ macOS build completed"

# Build for Windows only
@windows:
    echo "🪟 Building for Windows..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target windows-amd64 \
        --{{build_mode}} \
        --output-dir {{dist_dir}}/platform/windows \
        --verbose={{verbose}}
    {{nu}} {{tools_dir}}/distribution/generate-distribution.nu \
        --version {{version}} \
        --platforms windows-amd64 \
        --variants {{variants}} \
        --output-dir {{dist_dir}} \
        --verbose={{verbose}}
    echo "✅ Windows build completed"

# Build for all supported platforms
@all-platforms:
    echo "🌍 Building for all supported platforms..."
    just linux
    just macos
    just windows
    echo "✅ All platform builds completed"

# Build for macOS Apple Silicon
@macos-arm64:
    echo "🍎 Building for macOS Apple Silicon..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target macos-arm64 \
        --{{build_mode}} \
        --output-dir {{dist_dir}}/platform/macos-arm64 \
        --verbose={{verbose}}
    echo "✅ macOS Apple Silicon build completed"

# Test platform-specific builds
@platform-test:
    #!/usr/bin/env bash
    echo "🧪 Testing platform-specific builds..."
    CURRENT_PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]')

    if [ "$CURRENT_PLATFORM" = "linux" ]; then
        just linux
        echo "Testing Linux build..."
        {{nu}} {{tools_dir}}/test/platform-test.nu --platform linux --dist-dir {{dist_dir}}
    elif [ "$CURRENT_PLATFORM" = "darwin" ]; then
        just macos
        echo "Testing macOS build..."
        {{nu}} {{tools_dir}}/test/platform-test.nu --platform macos --dist-dir {{dist_dir}}
    else
        echo "Platform testing not supported on $CURRENT_PLATFORM"
    fi
    echo "✅ Platform testing completed"

# Cross-compile for specific target
@cross-compile TARGET:
    echo "🔄 Cross-compiling for {{TARGET}}..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target {{TARGET}} \
        --{{build_mode}} \
        --output-dir {{dist_dir}}/platform/{{TARGET}} \
        --verbose={{verbose}}
    echo "✅ Cross-compilation for {{TARGET}} completed"

# Show available build targets
@targets:
    echo "🎯 Available Build Targets"
    echo "=========================="
    rustup target list --installed
    echo ""
    echo "Supported by this project:"
    echo "  x86_64-unknown-linux-gnu      # Linux x86_64"
    echo "  x86_64-apple-darwin           # macOS Intel"
    echo "  aarch64-apple-darwin          # macOS Apple Silicon"
    echo "  x86_64-pc-windows-msvc        # Windows x86_64"

# Install additional build targets
@install-targets:
    echo "📦 Installing additional build targets..."
    rustup target add x86_64-unknown-linux-gnu
    rustup target add x86_64-apple-darwin
    rustup target add aarch64-apple-darwin
    rustup target add x86_64-pc-windows-msvc
    echo "✅ Build targets installed"

# Platform-specific optimization builds
@optimize-native:
    echo "⚡ Building with native optimizations..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target native \
        --{{build_mode}} \
        --optimize-native \
        --output-dir {{dist_dir}}/platform/native \
        --verbose={{verbose}}
    echo "✅ Native optimized build completed"

# Build for container deployment
@container-platform:
    echo "🐳 Building for container deployment..."
    {{nu}} {{tools_dir}}/build/compile-platform.nu \
        --target x86_64-unknown-linux-musl \
        --{{build_mode}} \
        --static-link \
        --output-dir {{dist_dir}}/platform/container \
        --verbose={{verbose}}
    echo "✅ Container platform build completed"

# Show platform-specific information
@platform-info:
    echo "🌐 Platform Information"
    echo "======================"
    echo "Current platform: $(uname -s -m)"
    echo "Rust host triple: $(rustc -vV | grep host | cut -d' ' -f2)"
    echo "Available targets: $(rustup target list --installed | tr '\n' ' ')"
    echo ""
    echo "Build configuration:"
    echo "  Default target: {{rust_target}}"
    echo "  Build mode:     {{build_mode}}"
    echo "  Platforms:      {{platforms}}"
    echo ""
    echo "Platform directories:"
    ls -la {{dist_dir}}/platform/ 2>/dev/null || echo "  No platform builds found"

# Clean platform-specific artifacts
@platform-clean:
    echo "🧹 Cleaning platform artifacts..."
    rm -rf {{dist_dir}}/platform
    mkdir -p {{dist_dir}}/platform
    echo "✅ Platform artifacts cleaned"

# Platform-specific package creation
@platform-packages:
    #!/usr/bin/env bash
    echo "📦 Creating platform-specific packages..."
    for platform in linux macos windows; do
        if [ -d "{{dist_dir}}/platform/$platform" ]; then
            echo "Creating package for $platform..."
            {{nu}} {{tools_dir}}/package/create-platform-package.nu \
                --platform "$platform" \
                --source-dir "{{dist_dir}}/platform/$platform" \
                --output-dir "{{packages_dir}}/platform" \
                --version {{version}} \
                --verbose={{verbose}}
        fi
    done
    echo "✅ Platform-specific packages created"
