name: "🆙 Bump versions"

on:
  workflow_dispatch:
    inputs:
      bump-type:
        description: "Bump Type"
        required: true
        default: "patch"
        type: choice
        options:
          - patch
          - minor
          - major

jobs:
  bump-version:
    name: Bump version and create PR
    runs-on: ubuntu-latest
    outputs:
      new_version: ${{ steps.bump_versions.outputs.NEW_VERSION }}
      versions_match: ${{ steps.bump_versions.outputs.VERSIONS_MATCH }}
      pr_number: ${{ steps.create-pr.outputs.pull-request-number }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref }}

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

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

      - name: Install Poetry
        run: |
          curl -sSL https://install.python-poetry.org | python3 -

      - name: Bump versions and compare
        id: bump_versions
        run: |
          BUMP_TYPE=${{ inputs.bump-type }}
          echo "Bumping versions to $BUMP_TYPE"

          cd agenta-web
          npm version $BUMP_TYPE
          WEB_VERSION=$(npm pkg get version | tr -d '"')
          cd ..

          cd agenta-cli
          poetry version $BUMP_TYPE
          CLI_VERSION=$(poetry version -s)
          cd ..

          cd agenta-backend
          poetry version $BUMP_TYPE
          BACKEND_VERSION=$(poetry version -s)
          cd ..

          echo "WEB_VERSION=$WEB_VERSION"
          echo "CLI_VERSION=$CLI_VERSION"
          echo "BACKEND_VERSION=$BACKEND_VERSION"

          if [ "$WEB_VERSION" = "$CLI_VERSION" ] && [ "$CLI_VERSION" = "$BACKEND_VERSION" ]; then
            echo "VERSIONS_MATCH=true" >> $GITHUB_OUTPUT
            echo "NEW_VERSION=$WEB_VERSION" >> $GITHUB_OUTPUT
          else
            echo "VERSIONS_MATCH=false" >> $GITHUB_OUTPUT
          fi

      - name: Create Pull Request
        if: steps.bump_versions.outputs.VERSIONS_MATCH == 'true'
        uses: peter-evans/create-pull-request@v6
        with:
          commit-message: v${{ steps.bump_versions.outputs.NEW_VERSION }}
          author: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com>
          branch: release/v${{ steps.bump_versions.outputs.NEW_VERSION }}
          delete-branch: true
          title: "v${{ steps.bump_versions.outputs.NEW_VERSION }}"
          body: |
            New version v${{ steps.bump_versions.outputs.NEW_VERSION }} in
            - agenta-web
            - agenta-backend
            - agenta-cli

      - name: Fail if versions don't match
        if: steps.bump_versions.outputs.VERSIONS_MATCH != 'true'
        run: |
          echo "Versions in the three folders do not match. Please check and update manually."
          exit 1

  create-tag:
    needs: bump-version
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Create and push tag
        run: |
          git config --global user.name "${{ github.actor }}"
          git config --global user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
          git tag -a "v${{ needs.bump-version.outputs.new_version }}" -m "Version ${{ needs.bump-version.outputs.new_version }}"
          git push origin "v${{ needs.bump-version.outputs.new_version }}"
