name: Publish to MCP Registry

on:
  push:
    tags: ["v*"]
  workflow_dispatch:

jobs:
  publish:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    permissions:
      id-token: write
      contents: read

    steps:
      - name: Checkout code
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: "20"
          registry-url: "https://registry.npmjs.org"
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Build package
        run: npm run build

      - name: Run linting
        run: npm run lint

      - name: Run smoke tests
        run: npm run test:smoke

      - name: Extract version from tag
        id: version
        env:
          EVENT_NAME: ${{ github.event_name }}
          REF_NAME: ${{ github.ref }}
        run: |
          if [[ "$EVENT_NAME" == "push" && "$REF_NAME" == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/v}
          else
            VERSION=$(node -p "require('./package.json').version")
          fi
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Publishing version: $VERSION"

      - name: Update server.json version
        env:
          VERSION: ${{ steps.version.outputs.version }}
        run: |
          jq --arg v "$VERSION" '.version = $v | .packages[] |= if .registryType == "npm" then .version = $v else . end' server.json > tmp && mv tmp server.json
          echo "Updated server.json:"
          cat server.json

      - name: Check npm package version
        id: npm_version
        env:
          VERSION: ${{ steps.version.outputs.version }}
        run: |
          if npm view "unreal-engine-mcp-server@$VERSION" version >/dev/null 2>&1; then
            echo "exists=true" >> "$GITHUB_OUTPUT"
            echo "npm package version $VERSION already exists; skipping publish."
          else
            echo "exists=false" >> "$GITHUB_OUTPUT"
            echo "npm package version $VERSION does not exist; publishing."
          fi

      - name: Publish to npm
        if: ${{ steps.npm_version.outputs.exists != 'true' }}
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Install MCP Publisher
        env:
          MCP_PUBLISHER_VERSION: v1.7.9
        run: |
          curl -L "https://github.com/modelcontextprotocol/registry/releases/download/${MCP_PUBLISHER_VERSION}/mcp-publisher_$(uname -s | tr '[:upper:]' '[:lower:]')_$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/').tar.gz" | tar xz mcp-publisher
          chmod +x mcp-publisher

      - name: Login to MCP Registry
        run: ./mcp-publisher login github-oidc --registry https://registry.modelcontextprotocol.io

      - name: Publish to MCP Registry
        run: ./mcp-publisher publish

      - name: Verify publication
        if: ${{ always() }}
        env:
          VERSION: ${{ steps.version.outputs.version }}
        run: |
          echo "Waiting for registry to update..."
          for attempt in {1..6}; do
            response=$(curl -fsS "https://registry.modelcontextprotocol.io/v0/servers?search=io.github.ChiR24/unreal-engine-mcp")
            if echo "$response" | jq -e --arg v "$VERSION" 'any(.servers[]?.server.version; . == $v)' >/dev/null; then
              echo "MCP Registry contains version $VERSION."
              echo "$response" | jq '.'
              exit 0
            fi
            echo "MCP Registry does not list version $VERSION yet (attempt $attempt/6)."
            sleep 20
          done
          echo "MCP Registry did not list version $VERSION after waiting."
          echo "$response" | jq '.'
          exit 1
