name: Publish to npm

on:
  # Uncomment to auto-publish on release:
  # release:
  #   types: [published]
  workflow_dispatch:
    inputs:
      tag:
        description: "Release tag to publish (e.g. v0.9.5)"
        required: true
        type: string

permissions:
  id-token: write
  contents: read

jobs:
  publish:
    name: Publish npm packages
    if: github.repository == 'nubo-db/dynoxide'
    runs-on: ubuntu-latest
    timeout-minutes: 15

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "24"

      - name: Configure npm registry
        run: npm config set registry https://registry.npmjs.org

      - name: Install jq
        run: sudo apt-get install -y jq

      - name: Determine version
        id: version
        env:
          INPUT_TAG: ${{ inputs.tag }}
          RELEASE_TAG: ${{ github.event.release.tag_name }}
        run: |
          if [[ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]]; then
            TAG="$INPUT_TAG"
          else
            TAG="$RELEASE_TAG"
          fi

          # Validate tag format
          if ! [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$ ]]; then
            echo "::error::Invalid tag format: $TAG (expected vX.Y.Z)"
            exit 1
          fi

          VERSION="${TAG#v}"
          echo "tag=$TAG" >> "$GITHUB_OUTPUT"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "Publishing version: $VERSION (tag: $TAG)"

      - name: Publish to npm
        env:
          PUBLISH_VERSION: ${{ steps.version.outputs.version }}
          PUBLISH_TAG: ${{ steps.version.outputs.tag }}
        run: |
          npm/scripts/publish.sh \
            --version "$PUBLISH_VERSION" \
            --release-url "https://github.com/nubo-db/dynoxide/releases/download/$PUBLISH_TAG"
