# Package Module - Distribution packaging
# =======================================

# Show detailed package help
@package-help:
    echo "📦 PACKAGE MODULE HELP"
    echo "======================"
    echo ""
    echo "This module handles distribution packaging including:"
    echo "• Binary packaging for multiple platforms"
    echo "• Container image building"
    echo "• Distribution archive creation"
    echo "• Installation package generation"
    echo ""
    echo "RECIPES:"
    echo "  package-all        Create all distribution packages"
    echo "  package-binaries   Package binaries for distribution"
    echo "  package-containers Build container images"
    echo "  create-archives    Create distribution archives"
    echo "  create-installers  Create installation packages"
    echo "  dist-generate      Generate complete distributions"
    echo ""
    echo "CONFIGURATION:"
    echo "  platforms: {{platforms}}"
    echo "  variants:  {{variants}}"
    echo "  version:   {{version}}"
    echo ""
    echo "EXAMPLES:"
    echo "  just package-all                  # Create all packages"
    echo "  just package-containers           # Build container images"
    echo "  just create-archives              # Create archives only"

# Create all distribution packages
@package-all: dist-generate
    echo "✅ All distribution packages created successfully"

# Generate complete distributions
@dist-generate:
    #!/usr/bin/env bash
    echo "📦 Generating distributions (skipped - Nushell parser issue in 0.109.1)..."
    # NOTE: package-binaries and package-containers are currently disabled due to
    # Nushell 0.109.1 parser issues with the generated function definitions.
    # These scripts have spacing issues that cause "missing positional" errors.
    # TODO: Reformat all function definitions in:
    #   - provisioning/tools/package/package-binaries.nu
    #   - provisioning/tools/package/build-containers.nu
    #   - provisioning/tools/distribution/generate-distribution.nu
    echo "✅ Distributions generated (placeholder)"

# Create distribution archives
@create-archives:
    echo "📂 Creating distribution archives..."
    {{nu}} {{tools_dir}}/package/create-tarball.nu \
        --dist-dir {{dist_dir}} \
        --output-dir {{packages_dir}} \
        --format both \
        --platform all \
        --variant complete \
        --version {{version}} \
        --compression-level 6 \
        --checksum \
        --verbose={{verbose}}
    echo "✅ Distribution archives created successfully"

# Create installation packages
@create-installers:
    #!/usr/bin/env bash
    echo "📥 Creating installers..."
    for dist in {{dist_dir}}/provisioning-{{version}}-*-complete; do
        if [ -d "$dist" ]; then
            {{nu}} {{tools_dir}}/distribution/create-installer.nu \
                "$dist" \
                --output-dir {{packages_dir}}/installers \
                --installer-types shell,package \
                --platforms linux,macos,windows \
                --include-services \
                --create-uninstaller \
                --validate-installer \
                --verbose={{verbose}} || exit 1
        fi
    done
    echo "✅ Installers created successfully"

# Validate packages
@package-validate:
    echo "🔍 Validating packages..."
    {{nu}} {{tools_dir}}/package/validate-package.nu {{dist_dir}} \
        --validation-type complete \
        --check-signatures \
        --verbose={{verbose}}
    echo "✅ Package validation completed"

# Show package statistics
@package-stats:
    echo "📊 Package Statistics"
    echo "===================="
    echo "Distribution directory: {{dist_dir}}"
    echo "Packages directory:     {{packages_dir}}"
    echo ""
    du -sh {{dist_dir}} {{packages_dir}} 2>/dev/null || echo "Directories not found"
    echo ""
    find {{packages_dir}} -name "*.tar.gz" -o -name "*.zip" -o -name "*.pkg" 2>/dev/null | wc -l | xargs echo "Package files:"

# Clean package artifacts
@package-clean:
    echo "🧹 Cleaning package artifacts..."
    rm -rf {{packages_dir}}
    mkdir -p {{packages_dir}}
    echo "✅ Package artifacts cleaned"
