# Release Module - Release management
# ===================================

# Show detailed release help
@release-help:
    echo "🚀 RELEASE MODULE HELP"
    echo "======================"
    echo ""
    echo "This module handles release management including:"
    echo "• Complete release creation with version tagging"
    echo "• Draft releases for testing"
    echo "• Artifact upload to GitHub and registries"
    echo "• Release notifications"
    echo "• Package registry updates"
    echo ""
    echo "RECIPES:"
    echo "  release            Create complete release (VERSION required)"
    echo "  release-draft      Create draft release for testing"
    echo "  upload-artifacts   Upload release artifacts"
    echo "  notify-release     Send release notifications"
    echo "  update-registry    Update package manager registries"
    echo "  release-notes      Generate release notes"
    echo ""
    echo "REQUIREMENTS:"
    echo "  VERSION must be specified for releases"
    echo "  GitHub CLI (gh) must be configured"
    echo "  Registry credentials must be available"
    echo ""
    echo "EXAMPLES:"
    echo "  just release VERSION=2.1.0        # Complete release"
    echo "  just release-draft VERSION=2.1.0  # Draft release"
    echo "  just upload-artifacts              # Upload only"
    echo "  just release-notes VERSION=2.1.0  # Generate notes"

# Create a complete release (requires VERSION)
@release VERSION="":
    #!/usr/bin/env bash
    if [ -z "{{VERSION}}" ]; then
        echo "❌ Error: VERSION must be specified for release"
        echo "Usage: just release VERSION=2.1.0"
        exit 1
    fi
    echo "🚀 Creating release {{VERSION}}..."
    {{nu}} {{tools_dir}}/release/create-release.nu \
        --version {{VERSION}} \
        --asset-dir {{packages_dir}} \
        --generate-changelog \
        --push-tag \
        --auto-upload \
        --verbose={{verbose}}
    echo "✅ Release {{VERSION}} created successfully"

# Create a draft release
@release-draft VERSION="":
    #!/usr/bin/env bash
    if [ -z "{{VERSION}}" ]; then
        echo "❌ Error: VERSION must be specified for draft release"
        echo "Usage: just release-draft VERSION=2.1.0-rc1"
        exit 1
    fi
    echo "📝 Creating draft release {{VERSION}}..."
    {{nu}} {{tools_dir}}/release/create-release.nu \
        --version {{VERSION}} \
        --draft \
        --asset-dir {{packages_dir}} \
        --generate-changelog \
        --push-tag \
        --verbose={{verbose}}
    echo "✅ Draft release {{VERSION}} created successfully"

# Upload release artifacts
@upload-artifacts VERSION="":
    #!/usr/bin/env bash
    RELEASE_VERSION="{{VERSION}}"
    if [ -z "$RELEASE_VERSION" ]; then
        RELEASE_VERSION="{{version}}"
    fi
    echo "⬆️  Uploading release artifacts for v$RELEASE_VERSION..."
    {{nu}} {{tools_dir}}/release/upload-artifacts.nu \
        --artifacts-dir {{packages_dir}} \
        --release-tag "v$RELEASE_VERSION" \
        --targets github,docker \
        --verify-uploads \
        --verbose={{verbose}}
    echo "✅ Artifacts uploaded successfully"

# Send release notifications
@notify-release VERSION="":
    #!/usr/bin/env bash
    RELEASE_VERSION="{{VERSION}}"
    if [ -z "$RELEASE_VERSION" ]; then
        RELEASE_VERSION="{{version}}"
    fi
    echo "📢 Sending release notifications for v$RELEASE_VERSION..."
    {{nu}} {{tools_dir}}/release/notify-users.nu \
        --channels slack,discord \
        --release-version "$RELEASE_VERSION" \
        --urgent=false \
        --verbose={{verbose}}
    echo "✅ Release notifications sent"

# Update package manager registries
@update-registry VERSION="":
    #!/usr/bin/env bash
    RELEASE_VERSION="{{VERSION}}"
    if [ -z "$RELEASE_VERSION" ]; then
        RELEASE_VERSION="{{version}}"
    fi
    echo "📦 Updating package registries for v$RELEASE_VERSION..."
    {{nu}} {{tools_dir}}/release/update-registry.nu \
        --registries homebrew \
        --version "$RELEASE_VERSION" \
        --auto-commit \
        --verbose={{verbose}}
    echo "✅ Package registries updated"

# Generate release notes
@release-notes VERSION="":
    #!/usr/bin/env bash
    if [ -z "{{VERSION}}" ]; then
        echo "❌ Error: VERSION must be specified for release notes"
        echo "Usage: just release-notes VERSION=2.1.0"
        exit 1
    fi
    echo "📋 Generating release notes for {{VERSION}}..."
    {{nu}} {{tools_dir}}/release/generate-notes.nu \
        --version {{VERSION}} \
        --format markdown \
        --include-contributors \
        --include-commits \
        --output {{packages_dir}}/release-notes-{{VERSION}}.md \
        --verbose={{verbose}}
    echo "✅ Release notes generated: {{packages_dir}}/release-notes-{{VERSION}}.md"

# Pre-release checks
@release-check VERSION="":
    #!/usr/bin/env bash
    if [ -z "{{VERSION}}" ]; then
        echo "❌ Error: VERSION must be specified for release check"
        echo "Usage: just release-check VERSION=2.1.0"
        exit 1
    fi
    echo "🔍 Running pre-release checks for {{VERSION}}..."

    # Check if version tag already exists
    if git rev-parse "v{{VERSION}}" >/dev/null 2>&1; then
        echo "❌ Error: Tag v{{VERSION}} already exists"
        exit 1
    fi

    # Check if working directory is clean
    if ! git diff-index --quiet HEAD --; then
        echo "❌ Error: Working directory is not clean"
        git status --porcelain
        exit 1
    fi

    # Check if on main branch
    CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    if [ "$CURRENT_BRANCH" != "main" ] && [ "$CURRENT_BRANCH" != "master" ]; then
        echo "⚠️  Warning: Not on main/master branch (currently on: $CURRENT_BRANCH)"
    fi

    echo "✅ Pre-release checks passed for {{VERSION}}"

# Complete release workflow
@release-workflow VERSION="":
    #!/usr/bin/env bash
    if [ -z "{{VERSION}}" ]; then
        echo "❌ Error: VERSION must be specified for release workflow"
        echo "Usage: just release-workflow VERSION=2.1.0"
        exit 1
    fi

    echo "🚀 Starting complete release workflow for {{VERSION}}..."

    just release-check {{VERSION}}
    just clean
    just build-all
    just package-all
    just release {{VERSION}}
    just upload-artifacts {{VERSION}}
    just update-registry {{VERSION}}
    just notify-release {{VERSION}}

    echo "✅ Complete release workflow finished for {{VERSION}}"

# List recent releases
@release-list:
    echo "📋 Recent Releases"
    echo "=================="
    git tag --sort=-version:refname | head -10
