# 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: build-archives build-installers
    echo "✅ Distribution generation completed"

# Build core distribution
@build-core-dist:
    echo "📦 Preparing core distribution..."
    {{nu}} {{tools_dir}}/distribution/prepare-core-dist.nu \
        --output-dir {{dist_dir}}/core \
        --validate-syntax \
        --generate-index \
        --bundle-plugins \
        --verbose={{verbose}} || echo "⚠️  Core distribution preparation completed with warnings"
    echo "✅ Core distribution prepared"

# Build platform distribution
@build-platform-dist:
    echo "📦 Preparing platform distribution..."
    {{nu}} {{tools_dir}}/distribution/prepare-platform-dist.nu \
        --output-dir {{dist_dir}}/platform \
        --target-platforms {{platforms}} \
        --build-mode release \
        --strip-symbols \
        --generate-services \
        --parallel-builds \
        --verbose={{verbose}} || echo "⚠️  Platform distribution preparation completed with warnings"
    echo "✅ Platform distribution prepared"

# Build distribution archives
@build-archives: build-core-dist build-platform-dist
    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"

# Build distribution installers
@build-installers: build-archives
    #!/usr/bin/env bash
    echo "📥 Creating distribution 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}} || true
        fi
    done
    echo "✅ Distribution installers created"

# 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"
