name: Publish to PyPI

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to publish (leave empty to use pyproject.toml version)'
        required: false
        type: string

jobs:
  build:
    name: Build distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install build dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install build

      - name: Build package
        run: python -m build

      - name: Store distribution packages
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

  publish-to-pypi:
    name: Publish to PyPI
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/openbrowser-ai
    permissions:
      id-token: write

    steps:
      - name: Download distribution packages
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  publish-to-testpypi:
    name: Publish to TestPyPI
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch'
    environment:
      name: testpypi
      url: https://test.pypi.org/p/openbrowser-ai
    permissions:
      id-token: write

    steps:
      - name: Download distribution packages
        uses: actions/download-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

      - name: Publish to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          repository-url: https://test.pypi.org/legacy/

  publish-to-mcp-registry:
    name: Publish to MCP Registry
    needs: publish-to-pypi
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v4

      - name: Install mcp-publisher
        run: |
          curl -sSL "https://github.com/modelcontextprotocol/registry/releases/latest/download/mcp-publisher_linux_amd64.tar.gz" | tar xz mcp-publisher
          chmod +x mcp-publisher
          ./mcp-publisher --help

      - name: Update server.json version
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          python3 -c "
          import json
          with open('server.json') as f:
              data = json.load(f)
          data['version'] = '${VERSION}'
          for pkg in data.get('packages', []):
              pkg['version'] = '${VERSION}'
          with open('server.json', 'w') as f:
              json.dump(data, f, indent=2)
          "
          cat server.json

      - name: Authenticate to MCP Registry
        run: ./mcp-publisher login dns --domain=openbrowser.me --private-key=${{ secrets.MCP_DNS_PRIVATE_KEY }}

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

  sync-plugin-version:
    name: Sync Claude Code Plugin Version
    needs: publish-to-pypi
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          ref: main
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Update plugin version to match release
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          python3 -c "
          import json
          path = 'plugin/.claude-plugin/plugin.json'
          with open(path) as f:
              data = json.load(f)
          if data['version'] != '${VERSION}':
              data['version'] = '${VERSION}'
              with open(path, 'w') as f:
                  json.dump(data, f, indent=2)
                  f.write('\n')
              print(f'Updated plugin version to ${VERSION}')
          else:
              print(f'Plugin already at ${VERSION}, no update needed')
          "

      - name: Commit and push if changed
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add plugin/.claude-plugin/plugin.json
          git diff --cached --quiet && echo "No changes to commit" || \
            git commit -m "chore(plugin): sync version to ${GITHUB_REF_NAME#v}" && \
            git push origin main
