name: Version Bump

on:
  workflow_dispatch:
    inputs:
      version_type:
        description: 'Version bump type'
        required: true
        default: 'patch'
        type: choice
        options:
          - patch
          - minor
          - major
          - custom
      custom_version:
        description: 'Custom version (only for custom type)'
        required: false

jobs:
  bump-version:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          
      - name: Get current version
        id: current
        run: |
          CURRENT_VERSION=$(node -p "require('./package.json').version")
          echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
          
      - name: Calculate new version
        id: new
        run: |
          CURRENT="${{ steps.current.outputs.version }}"
          TYPE="${{ github.event.inputs.version_type }}"
          CUSTOM="${{ github.event.inputs.custom_version }}"
          
          if [ "$TYPE" = "custom" ]; then
            NEW_VERSION="$CUSTOM"
          else
            # Разбираем текущую версию
            IFS='.' read -r major minor patch <<< "$CURRENT"
            
            case "$TYPE" in
              major)
                NEW_VERSION="$((major + 1)).0.0"
                ;;
              minor)
                NEW_VERSION="$major.$((minor + 1)).0"
                ;;
              patch)
                NEW_VERSION="$major.$minor.$((patch + 1))"
                ;;
            esac
          fi
          
          echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
          
      - name: Update version
        run: |
          npm run version:sync ${{ steps.new.outputs.version }}
          
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: "chore: bump version to ${{ steps.new.outputs.version }}"
          title: "chore: bump version to ${{ steps.new.outputs.version }}"
          body: |
            ## Version Bump
            
            Bumps version from `${{ steps.current.outputs.version }}` to `${{ steps.new.outputs.version }}`
            
            ### Changes
            - Updated version in `package.json`
            - Updated version in `src-tauri/Cargo.toml`
            - Updated version in `src-tauri/tauri.conf.json`
            - Updated version in test mocks
            
            ### Checklist
            - [ ] Tests pass
            - [ ] Build succeeds
            - [ ] Changelog updated
            
            ---
            *This PR was automatically created by the Version Bump workflow*
          branch: version-bump/${{ steps.new.outputs.version }}
          delete-branch: true
          
  # Альтернативный job для прямого коммита (без PR)
  bump-version-direct:
    if: false # Включить если нужен прямой коммит
    runs-on: ubuntu-latest
    permissions:
      contents: write
      
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          
      - name: Configure Git
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          
      - name: Update and commit version
        run: |
          # Та же логика расчета версии
          # ...
          
          npm run version:sync $NEW_VERSION
          
          git add -A
          git commit -m "chore: bump version to $NEW_VERSION"
          git tag v$NEW_VERSION
          
          git push origin main
          git push origin v$NEW_VERSION