name: Release

on:
  workflow_dispatch:
    inputs:
      bump:
        description: 'Version bump type'
        required: true
        default: 'patch'
        type: choice
        options:
          - patch
          - minor
          - major

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    outputs:
      version: ${{ steps.bump.outputs.version }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          # NOTE: No registry-url - npm OIDC trusted publishers require no auth token present

      - name: Get current version
        id: current
        run: |
          VERSION=$(grep -Po '(?<=version = ")[^"]+' pyproject.toml)
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - name: Bump version
        id: bump
        env:
          CURRENT_VERSION: ${{ steps.current.outputs.version }}
          BUMP_TYPE: ${{ inputs.bump }}
        run: |
          IFS='.' read -r major minor patch <<< "$CURRENT_VERSION"
          case "$BUMP_TYPE" in
            major) major=$((major + 1)); minor=0; patch=0 ;;
            minor) minor=$((minor + 1)); patch=0 ;;
            patch) patch=$((patch + 1)) ;;
          esac
          NEW_VERSION="$major.$minor.$patch"
          echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
          sed -i "s/version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" pyproject.toml
          npm version "$NEW_VERSION" --no-git-tag-version

      - name: Commit and tag
        env:
          NEW_VERSION: ${{ steps.bump.outputs.version }}
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add pyproject.toml package.json
          git commit -m "(release): v$NEW_VERSION"
          git tag "v$NEW_VERSION"
          git push && git push --tags

      - name: Create release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NEW_VERSION: ${{ steps.bump.outputs.version }}
        run: gh release create "v$NEW_VERSION" --generate-notes

      # Publish to PyPI
      - name: Install build
        run: pip install build

      - name: Build Python package
        run: python -m build

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

      # Publish to npm (OIDC trusted publisher)
      - name: Upgrade npm for OIDC support
        run: npm install -g npm@latest && npm --version

      - name: Publish to npm
        run: npm publish --provenance --access public

  build-binaries:
    needs: release
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: linux-x64
          - os: macos-latest
            target: darwin-arm64
          - os: macos-15-intel
            target: darwin-x64
    steps:
      - uses: actions/checkout@v4
        with:
          ref: v${{ needs.release.outputs.version }}

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Build binary
        env:
          TARGET: ${{ matrix.target }}
        run: bun build --compile mcpmon.ts --outfile "mcpmon-$TARGET"

      - name: Upload to release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VERSION: ${{ needs.release.outputs.version }}
          TARGET: ${{ matrix.target }}
        run: gh release upload "v$VERSION" "mcpmon-$TARGET" --clobber
