name: Bump version

on:
  push:
    branches:
      - master

jobs:
  bump-version:
    if: "!startsWith(github.event.head_commit.message, 'bump:') && !startsWith(github.event.head_commit.message, 'chore(release):')"
    runs-on: ubuntu-latest
    name: "Bump version and create changelog"
    permissions:
      contents: write
    outputs:
      bumped: ${{ steps.bump.outputs.bumped }}
      tag: ${{ steps.bump.outputs.tag }}
    steps:
      - name: Check out
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: '3.11'

      - name: Install uv
        run: |
          curl -LsSf https://astral.sh/uv/install.sh | sh
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Configure git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Detect and bump version
        id: bump
        run: |
          set -euo pipefail

          echo "Checking for version bump..."

          # Get the most recent tag
          last_tag=$(git tag --sort=-creatordate | grep -E "^v[0-9]" | head -n 1 || echo "")

          if [ -z "$last_tag" ]; then
            commit_range="master"
          else
            commit_range="${last_tag}..HEAD"
          fi

          # Count conventional commits
          commit_count=$(git log "$commit_range" --oneline --grep="^(feat|fix|docs|refactor|perf|test|build|ci|chore)" -E | wc -l)

          if [ "$commit_count" -gt 0 ]; then
            echo "Found $commit_count commits since $last_tag"
            echo "Bumping version..."
            ./scripts/bump-mcp.sh
            echo "bumped=true" >> $GITHUB_OUTPUT
            tag=$(git tag --sort=-creatordate | grep -E '^v[0-9]' | head -n 1)
            echo "tag=$tag" >> $GITHUB_OUTPUT
          else
            echo "No commits found since $last_tag"
            echo "bumped=false" >> $GITHUB_OUTPUT
          fi

      - name: Push tags
        if: steps.bump.outputs.bumped == 'true'
        run: |
          git push
          git push --tags

      - name: Summary
        run: |
          if [ "${{ steps.bump.outputs.bumped }}" == "true" ]; then
            tag=$(git tag --sort=-creatordate | grep -E '^v[0-9]' | head -n 1)
            echo "## Version Bump Summary" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "- **MCP Server**: \`$tag\`" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "Tag has been pushed and release workflows will trigger automatically." >> $GITHUB_STEP_SUMMARY
          else
            echo "## Version Bump Summary" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "No version bump required - no relevant commits found since last release." >> $GITHUB_STEP_SUMMARY
          fi

  release:
    needs: bump-version
    if: needs.bump-version.outputs.bumped == 'true'
    uses: ./.github/workflows/release.yml
    with:
      tag: ${{ needs.bump-version.outputs.tag }}
    permissions:
      id-token: write
      contents: read

  docker:
    needs: bump-version
    if: needs.bump-version.outputs.bumped == 'true'
    uses: ./.github/workflows/docker-build-publish.yml
    with:
      tag: ${{ needs.bump-version.outputs.tag }}
    permissions:
      contents: read
      packages: write
