# ╔══════════════════════════════════════════════════════════════════════╗
# ║                   DISTRIBUTION & PACKAGING RECIPES                   ║
# ║              Targets, cross-compilation, and distribution            ║
# ╚══════════════════════════════════════════════════════════════════════╝

# Help for distro module
distro-help:
    @echo "DISTRIBUTION & TARGET MODULE"
    @echo ""
    @echo "Targets:"
    @echo "  just distro::list-targets          List installed Rust targets"
    @echo "  just distro::install-targets       Install common cross-compile targets"
    @echo "  just distro::install-target TGT    Install specific target"
    @echo ""
    @echo "Build:"
    @echo "  just distro::build-target TGT      Build for specific target"
    @echo "  just distro::build-all-targets     Build for all installed targets"
    @echo ""
    @echo "Installation:"
    @echo "  just distro::install               Install binaries to ~/.local/bin"
    @echo "  just distro::install DIR=/path     Install to custom directory"
    @echo ""
    @echo "Utilities:"
    @echo "  just distro::clean-targets         Clean target build artifacts"

# Workspace root directory - justfile_directory() returns vapora directory when called from module
WORKSPACE_ROOT := justfile_directory()

# === TARGETS ===

# List all installed Rust targets
[doc("List installed Rust targets")]
list-targets:
    @echo "=== Installed Rust Targets ==="
    rustup target list | grep installed
    @echo ""
    @echo "Common VAPORA targets:"
    @echo "  x86_64-unknown-linux-gnu      Linux x86_64 (default)"
    @echo "  x86_64-apple-darwin           macOS Intel"
    @echo "  aarch64-apple-darwin          macOS ARM64 (Apple Silicon)"
    @echo "  aarch64-unknown-linux-gnu     Linux ARM64"
    @echo "  x86_64-pc-windows-gnu         Windows x86_64"

# Install common cross-compilation targets
[doc("Install common cross-compile targets")]
install-targets:
    #!/usr/bin/env bash
    set -e
    echo "=== Installing Rust targets ==="
    TARGETS=(
        "x86_64-unknown-linux-gnu"
        "x86_64-apple-darwin"
        "aarch64-apple-darwin"
        "aarch64-unknown-linux-gnu"
        "x86_64-pc-windows-gnu"
    )
    for target in "${TARGETS[@]}"; do
        echo "Installing $target..."
        rustup target add "$target" || echo "  ⊘ $target (already installed)"
    done
    echo "✓ Targets installed"

# Install specific Rust target
[doc("Install specific Rust target")]
install-target TARGET:
    #!/usr/bin/env bash
    set -e
    echo "=== Installing target: {{TARGET}} ==="
    rustup target add "{{TARGET}}"
    echo "✓ {{TARGET}} installed"

# === BUILD ===

# Build for specific target
[doc("Build for specific target")]
build-target TARGET:
    #!/usr/bin/env bash
    set -e
    echo "=== Building for {{TARGET}} (release) ==="
    cd "{{ WORKSPACE_ROOT }}"
    cargo build --release --target "{{TARGET}}" --workspace
    echo "✓ Build complete for {{TARGET}}"

# Build for all installed targets
[doc("Build for all installed targets")]
build-all-targets:
    #!/usr/bin/env bash
    set -e
    echo "=== Building for all targets ==="
    cd "{{ WORKSPACE_ROOT }}"
    TARGETS=$(rustup target list | grep installed | awk '{print $1}')
    for target in $TARGETS; do
        echo ""
        echo "Building for $target..."
        cargo build --release --target "$target" --workspace || {
            echo "✗ Build failed for $target"
            continue
        }
        echo "✓ $target complete"
    done
    echo ""
    echo "✓ All target builds complete"

# === INSTALLATION ===

# Build and install release binaries (default: ~/.local/bin)
[doc("Build and install binaries (default: ~/.local/bin or DIR=<path>)")]
install DIR="":
    #!/bin/bash
    set -e

    # Normalize workspace path using shell
    WORKSPACE="{{ WORKSPACE_ROOT }}"
    WORKSPACE="$(cd "$WORKSPACE" && pwd)" || { echo "✗ Failed to access workspace"; exit 1; }

    # Resolve install directory
    if [ -z "{{ DIR }}" ]; then
        INSTALL_DIR="$HOME/.local/bin"
    else
        INSTALL_DIR="{{ DIR }}"
    fi

    echo "=== Building and Installing VAPORA binaries ==="
    echo "Build workspace: $WORKSPACE"
    echo "Install directory: $INSTALL_DIR"
    echo ""

    # First, build release binaries
    echo "📦 Building release binaries..."
    cd "$WORKSPACE" || exit 1
    cargo build --release -p vapora-backend -p vapora-agents --quiet
    echo "✓ Build complete"
    echo ""

    # Create installation directory
    mkdir -p "$INSTALL_DIR" || { echo "✗ Failed to create directory"; exit 1; }
    echo "Installing binaries to $INSTALL_DIR..."

    # Track installations
    INSTALLED=0

    # Define all binaries to install
    declare -a BINARIES=(
        "vapora-backend"
        "vapora-agents"
        "vapora-mcp-server"
        "vapora"
    )

    # Install each binary
    for BIN_NAME in "${BINARIES[@]}"; do
        BIN_PATH="$WORKSPACE/target/release/$BIN_NAME"

        if [ -f "$BIN_PATH" ]; then
            cp "$BIN_PATH" "$INSTALL_DIR/"
            chmod +x "$INSTALL_DIR/$BIN_NAME"
            echo "  ✓ $BIN_NAME"
            INSTALLED=$((INSTALLED + 1))
        fi
    done

    # Show warning if no binaries found
    if [ $INSTALLED -eq 0 ]; then
        echo "  ⊘ No binaries found in $WORKSPACE/target/release"
    fi

    echo ""
    if [ $INSTALLED -eq 0 ]; then
        echo "✗ No binaries were installed"
        exit 1
    fi

    echo "✓ Installation complete ($INSTALLED binaries installed)"
    echo ""
    echo "📋 Installation summary:"
    echo "   Install dir: $INSTALL_DIR"
    echo "   Binaries: $(ls -1 "$INSTALL_DIR"/vapora-* 2>/dev/null | xargs -I {} basename {})"
    echo ""

    if echo "$INSTALL_DIR" | grep -q "\.local/bin"; then
        echo "⚠️  Shell setup required:"
        echo "   export PATH=\"\$PATH:$INSTALL_DIR\""
        echo "   Or add to ~/.bashrc/.zshrc for persistent setup"
    fi

    echo ""
    echo "🧪 Verify installation:"
    echo "   $INSTALL_DIR/vapora-backend --help"
    echo "   $INSTALL_DIR/vapora-agents --help"
    echo ""

# === UTILITIES ===

# Clean target build artifacts
[doc("Clean all target build artifacts")]
clean-targets:
    @echo "=== Cleaning target artifacts ==="
    cd "{{ WORKSPACE_ROOT }}" && cargo clean
    @echo "✓ Cleaned all targets"
