name: Bump Version and Release

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

jobs:
  bump-and-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITEA_TOKEN }}
          fetch-depth: 0

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

      - name: Bump version
        id: bump
        run: |
          python bump_version.py ${{ github.event.inputs.bump_type }}
          NEW_VERSION=$(grep -oP 'version = "\K[^"]+' pyproject.toml)
          echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
          echo "tag=v$NEW_VERSION" >> $GITHUB_OUTPUT

      - name: Commit and tag
        run: |
          git config user.name "gitea-actions[bot]"
          git config user.email "gitea-actions[bot]@users.noreply.gitea.io"
          git add pyproject.toml
          git commit -m "bump version to v${{ steps.bump.outputs.version }}"
          git tag v${{ steps.bump.outputs.version }}

      - name: Push changes
        run: |
          git push origin main
          git push origin v${{ steps.bump.outputs.version }}
