name: Publish

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      tag:
        description: "Tag to publish (e.g. v2.9.3)"
        required: true
        type: string

concurrency:
  group: publish
  cancel-in-progress: false

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    environment: npm-publish
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.0
        with:
          ref: ${{ github.event.inputs.tag || github.ref_name }}

      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.0.0
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org

      - name: Install dependencies
        run: npm ci --force

      - name: Verify release tag matches package version
        env:
          TAG_REF: ${{ github.event.inputs.tag || github.ref_name }}
        run: |
          PKG_VERSION=$(node -p "require('./package.json').version")
          TAG_VERSION="${TAG_REF#v}"
          if [ "$PKG_VERSION" != "$TAG_VERSION" ]; then
            echo "::error::package.json version ($PKG_VERSION) does not match tag ($TAG_VERSION)"
            exit 1
          fi

      - name: Check if version already published
        id: check
        run: |
          PACKAGE_VERSION=$(node -p "require('./package.json').version")
          if npm view safari-mcp@$PACKAGE_VERSION version 2>/dev/null; then
            echo "exists=true" >> $GITHUB_OUTPUT
            echo "Version $PACKAGE_VERSION already published — skipping"
          else
            echo "exists=false" >> $GITHUB_OUTPUT
            echo "Version $PACKAGE_VERSION not found — will publish"
          fi

      - name: Publish to npm
        if: steps.check.outputs.exists == 'false'
        run: npm publish --provenance --access public

      - name: Sync server.json version to match package.json
        run: |
          node -e "
            const fs = require('fs');
            const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
            const s = JSON.parse(fs.readFileSync('server.json', 'utf8'));
            s.version = pkg.version;
            if (Array.isArray(s.packages)) {
              for (const p of s.packages) p.version = pkg.version;
            }
            fs.writeFileSync('server.json', JSON.stringify(s, null, 2) + '\n');
          "
          cat server.json

      - name: Download mcp-publisher
        run: |
          OS=$(uname -s | tr '[:upper:]' '[:lower:]')
          ARCH=$(uname -m)
          if [ "$ARCH" = "x86_64" ]; then ARCH=amd64; fi
          if [ "$ARCH" = "aarch64" ]; then ARCH=arm64; fi
          curl -sL "https://github.com/modelcontextprotocol/registry/releases/latest/download/mcp-publisher_${OS}_${ARCH}.tar.gz" -o mcp-publisher.tar.gz
          tar -xzf mcp-publisher.tar.gz mcp-publisher
          chmod +x mcp-publisher
          ./mcp-publisher --help | head -3

      - name: Publish to MCP Registry
        run: |
          ./mcp-publisher login github-oidc
          ./mcp-publisher publish
