#!/bin/bash

set -e

echo "=== Add Box Release Tag Script ==="
echo "Timestamp: $(date)"
echo ""

echo "=== Environment Variables ==="
echo "BOX_RELEASE_TAG: ${BOX_RELEASE_TAG:-'(not set)'}"
echo "CI_TOOLS_VERSION: ${CI_TOOLS_VERSION:-'(not set)'}"
echo "NODE_ENV: ${NODE_ENV:-'(not set)'}"
echo "npm_config_registry: ${npm_config_registry:-'(not set)'}"
echo ""

VERSION="$(ci_tools get-version)"
echo "=== Version Information ==="
echo "Detected version: $VERSION"
echo ""

PACKAGE_NAME="@5minds/processcube_engine_client"

echo "=== Current NPM Package Information ==="
pnpm view "$PACKAGE_NAME@$VERSION" --json || echo "Package version not found in registry"
echo ""

echo "=== Adding dist-tag ==="
echo "Package: $PACKAGE_NAME"
echo "Version: $VERSION" 
echo "Tag: ${BOX_RELEASE_TAG}"
echo ""

if [ -z "$BOX_RELEASE_TAG" ]; then
    echo "Error: BOX_RELEASE_TAG environment variable is not set"
    exit 1
fi

pnpm dist-tag add "$PACKAGE_NAME@$VERSION" "$BOX_RELEASE_TAG"

echo ""
echo "=== Verification ==="
pnpm dist-tag ls "$PACKAGE_NAME"
echo ""
echo "✅ Successfully added dist-tag '$BOX_RELEASE_TAG' to $PACKAGE_NAME@$VERSION"
