# Upstream Module - Repository Tracking and Synchronization
# Commands for managing upstream repositories and automated tracking

# 🔍 UPSTREAM CHECK COMMANDS

# Check upstream changes for all plugins
[no-cd]
upstream-check:
    @echo "🔍 Checking upstream changes..."
    @{{justfile_directory()}}/scripts/run.sh check_upstream_changes.nu

# Check upstream for specific plugin
[no-cd]
upstream-check-plugin PLUGIN:
    @echo "🔍 Checking upstream for {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh check_upstream_changes.nu --plugin {{PLUGIN}}

# Check upstream with verbose output
[no-cd]
upstream-check-verbose:
    @echo "🔍 Checking upstream changes (verbose)..."
    @{{justfile_directory()}}/scripts/run.sh check_upstream_changes.nu --verbose

# Check only plugins with pending status
[no-cd]
upstream-check-pending:
    @echo "🔍 Checking only pending plugins..."
    @{{justfile_directory()}}/scripts/run.sh check_upstream_changes.nu --pending-only

# 👀 UPSTREAM PREVIEW COMMANDS

# Preview merge changes for a plugin
[no-cd]
upstream-preview PLUGIN:
    @echo "👀 Preview merge for {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --preview {{PLUGIN}}

# Preview all pending merges
[no-cd]
upstream-preview-all:
    @echo "👀 Preview all pending merges..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --preview --all

# Show diff for plugin
[no-cd]
upstream-diff PLUGIN:
    @echo "📋 Showing diff for {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --diff {{PLUGIN}}

# 🔀 UPSTREAM MERGE COMMANDS

# Safely merge upstream changes for a plugin
[no-cd]
upstream-merge PLUGIN:
    @echo "🔀 Merging upstream changes for {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu {{PLUGIN}}

# Merge all pending upstream changes
[no-cd]
upstream-merge-all:
    @echo "🔀 Merging all pending upstream changes..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --all

# Force merge upstream for a plugin (even if status is OK)
[no-cd]
upstream-merge-force PLUGIN:
    @echo "💪 Force merging {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --force {{PLUGIN}}

# Merge with auto-approval of dependency-only changes
[no-cd]
upstream-merge-auto PLUGIN:
    @echo "🤖 Auto-merging {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --auto-approve {{PLUGIN}}

# Merge all with auto-approval
[no-cd]
upstream-merge-auto-all:
    @echo "🤖 Auto-merging all pending changes..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --auto-approve --all

# 🔙 UPSTREAM ROLLBACK COMMANDS

# Rollback last merge for plugin
[no-cd]
upstream-rollback PLUGIN:
    @echo "🔙 Rolling back {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --rollback {{PLUGIN}}

# Show rollback options for plugin
[no-cd]
upstream-rollback-info PLUGIN:
    @echo "ℹ️ Rollback info for {{PLUGIN}}..."
    @{{justfile_directory()}}/scripts/run.sh safe_merge_upstream.nu --rollback-info {{PLUGIN}}

# 📊 UPSTREAM STATUS COMMANDS

# Show upstream status for all plugins
[no-cd]
upstream-status:
    @echo "📊 Upstream Status"
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu

# Show detailed upstream status
[no-cd]
upstream-status-detailed:
    @echo "📊 Detailed Upstream Status"
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu --all

# Show plugins requiring attention
[no-cd]
upstream-attention:
    @echo "🚨 Plugins Requiring Attention"
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu attention

# Show upstream summary
[no-cd]
upstream-summary:
    @echo "📊 Upstream Summary"
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu summary

# Update plugin status manually
upstream-status-update PLUGIN STATUS:
    @echo "🔄 Updating {{PLUGIN}} status to {{STATUS}}..."
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu update {{PLUGIN}} {{STATUS}}

# Mark local development plugins as OK
[no-cd]
upstream-mark-locals-ok:
    @echo "✅ Marking local development plugins as OK..."
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu update nu_plugin_image ok
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu update nu_plugin_hashes ok
    @{{justfile_directory()}}/scripts/run.sh plugin_status.nu update nu_plugin_desktop_notifications ok
    @echo "✅ All local plugins marked as OK"

# 🔧 UPSTREAM CONFIGURATION

# Show plugin registry information
[no-cd]
upstream-registry:
    @echo "📋 Plugin Registry Information:"
    @if [ -f etc/plugin_registry.toml ]; then \
        echo "Registry file: etc/plugin_registry.toml"; \
        echo ""; \
        echo "Plugins with upstream:"; \
        grep -A2 "upstream_url.*=" etc/plugin_registry.toml | grep -v "^--" || true; \
    else \
        echo "Registry file not found!"; \
    fi

# Add plugin to upstream exclusion list
[no-cd]
upstream-exclude PLUGIN:
    @echo "🚫 Adding {{PLUGIN}} to exclusion list..."
    @EXCLUDE_FILE="etc/upstream_exclude.toml"; \
    if grep -q "^plugins = \[" "$$EXCLUDE_FILE" 2>/dev/null; then \
        sed -i.bak "/^plugins = \[/s/\]/\"{{PLUGIN}}\", \]/" "$$EXCLUDE_FILE"; \
        echo "✅ Added {{PLUGIN}} to exclusions"; \
    else \
        echo "⚠️  Please manually add {{PLUGIN}} to $$EXCLUDE_FILE"; \
    fi

# Remove plugin from upstream exclusion list
[no-cd]
upstream-include PLUGIN:
    @echo "✅ Removing {{PLUGIN}} from exclusion list..."
    @EXCLUDE_FILE="etc/upstream_exclude.toml"; \
    sed -i.bak "s/\"{{PLUGIN}}\",*[[:space:]]*//" "$$EXCLUDE_FILE"; \
    echo "✅ Removed {{PLUGIN}} from exclusions"

# Show excluded plugins
[no-cd]
upstream-excluded:
    @echo "📋 Currently excluded plugins:"
    @EXCLUDE_FILE="etc/upstream_exclude.toml"; \
    if [ -f "$$EXCLUDE_FILE" ]; then \
        grep -A10 "\[exclude\]" "$$EXCLUDE_FILE" | grep -E "^[[:space:]]*\"" | sed 's/[",]//g' | sed 's/^[[:space:]]*/  - /' || echo "No exclusions found"; \
    else \
        echo "No exclusions file found"; \
    fi

# 🔄 UPSTREAM MAINTENANCE

# Fetch all upstream repositories
[no-cd]
upstream-fetch-all:
    @echo "🔄 Fetching all upstream repositories..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin/.git" ]; then \
            echo "Fetching $$plugin..."; \
            cd "$$plugin" && git fetch --all && cd ..; \
        fi; \
    done

# Show outdated upstream references
[no-cd]
upstream-outdated:
    @echo "📅 Checking for outdated upstream references..."
    @{{justfile_directory()}}/scripts/run.sh check_upstream_changes.nu --show-outdated

# Cleanup upstream tracking data
[no-cd]
upstream-cleanup:
    @echo "🧹 Cleaning up upstream tracking data..."
    @find . -name ".upstream_*" -type f -delete
    @echo "✅ Cleanup completed"

# 🚨 UPSTREAM CONFLICT RESOLUTION

# Show conflicts for plugin
[no-cd]
upstream-conflicts PLUGIN:
    @echo "🚨 Showing conflicts for {{PLUGIN}}..."
    @cd {{PLUGIN}} && git status --porcelain | grep "^UU" || echo "No conflicts found"

# Reset plugin to clean state
[no-cd]
upstream-reset PLUGIN:
    @echo "🔄 Resetting {{PLUGIN}} to clean state..."
    @cd {{PLUGIN}} && git reset --hard HEAD && git clean -fd

# Create backup branch before merge
[no-cd]
upstream-backup PLUGIN:
    @echo "💾 Creating backup branch for {{PLUGIN}}..."
    @cd {{PLUGIN}} && git branch "backup-$(date +%Y%m%d-%H%M%S)" && echo "✅ Backup branch created"

# 📊 UPSTREAM INFORMATION

# Show upstream help
[no-cd]
upstream-help:
    @echo "🔄 Upstream Module Help"
    @echo "======================"
    @echo ""
    @echo "CHECK COMMANDS:"
    @echo "  upstream-check           - Check all upstream changes"
    @echo "  upstream-check-plugin PLUGIN - Check specific plugin"
    @echo "  upstream-check-verbose   - Check with verbose output"
    @echo "  upstream-check-pending   - Check only pending plugins"
    @echo ""
    @echo "PREVIEW COMMANDS:"
    @echo "  upstream-preview PLUGIN  - Preview merge changes"
    @echo "  upstream-preview-all     - Preview all pending merges"
    @echo "  upstream-diff PLUGIN     - Show diff for plugin"
    @echo ""
    @echo "MERGE COMMANDS:"
    @echo "  upstream-merge PLUGIN    - Merge upstream changes"
    @echo "  upstream-merge-all       - Merge all pending changes"
    @echo "  upstream-merge-force PLUGIN - Force merge"
    @echo "  upstream-merge-auto PLUGIN - Auto-merge dependencies"
    @echo "  upstream-merge-auto-all  - Auto-merge all dependencies"
    @echo ""
    @echo "ROLLBACK COMMANDS:"
    @echo "  upstream-rollback PLUGIN - Rollback last merge"
    @echo "  upstream-rollback-info PLUGIN - Show rollback info"
    @echo ""
    @echo "STATUS COMMANDS:"
    @echo "  upstream-status          - Show status for all plugins"
    @echo "  upstream-status-detailed - Show detailed status"
    @echo "  upstream-attention       - Show plugins needing attention"
    @echo "  upstream-summary         - Show summary"
    @echo "  upstream-status-update PLUGIN STATUS - Update status"
    @echo "  upstream-mark-locals-ok  - Mark local plugins as OK"
    @echo ""
    @echo "CONFIGURATION:"
    @echo "  upstream-registry        - Show registry information"
    @echo "  upstream-exclude PLUGIN  - Add to exclusion list"
    @echo "  upstream-include PLUGIN  - Remove from exclusion list"
    @echo "  upstream-excluded        - Show excluded plugins"
    @echo ""
    @echo "MAINTENANCE:"
    @echo "  upstream-fetch-all       - Fetch all repositories"
    @echo "  upstream-outdated        - Show outdated references"
    @echo "  upstream-cleanup         - Clean tracking data"
    @echo ""
    @echo "CONFLICT RESOLUTION:"
    @echo "  upstream-conflicts PLUGIN - Show conflicts"
    @echo "  upstream-reset PLUGIN    - Reset to clean state"
    @echo "  upstream-backup PLUGIN   - Create backup branch"
    @echo ""
    @echo "EXAMPLES:"
    @echo "  just upstream-check-plugin nu_plugin_clipboard"
    @echo "  just upstream-preview nu_plugin_highlight"
    @echo "  just upstream-merge-auto-all"
    @echo "  just upstream-exclude nu_plugin_custom"
