name: Version Check

on:
  pull_request:
    branches: [main]

jobs:
  check-version:
    name: Check version is bumped
    runs-on: ubuntu-latest
    steps:
      - name: Check if version bump required
        id: check-paths
        uses: dorny/paths-filter@v3
        with:
          filters: |
            needs-bump:
              - 'src/**'
              - 'Cargo.toml'
              - 'Cargo.lock'

      - name: Checkout
        if: steps.check-paths.outputs.needs-bump == 'true'
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Compare versions
        if: steps.check-paths.outputs.needs-bump == 'true'
        run: |
          # Get version from PR branch
          PR_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          
          # Get version from main branch
          git checkout origin/main -- Cargo.toml
          MAIN_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          
          # Restore PR version
          git checkout HEAD -- Cargo.toml
          
          echo "Main version: $MAIN_VERSION"
          echo "PR version:   $PR_VERSION"
          
          if [ "$PR_VERSION" = "$MAIN_VERSION" ]; then
            echo ""
            echo "::error::Version in Cargo.toml ($PR_VERSION) must be bumped before merging to main"
            echo ""
            echo "Run 'cargo release patch --execute' (or minor/major) on your branch to bump the version."
            exit 1
          fi
          
          echo ""
          echo "Version check passed: $MAIN_VERSION -> $PR_VERSION"

      - name: Skip version check
        if: steps.check-paths.outputs.needs-bump != 'true'
        run: echo "No source changes, skipping version check"
