name: Check Changelog

on:
  pull_request:
    paths:
      - 'pyproject.toml'
      - 'CHANGELOG.md'

jobs:
  check-changelog:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install dependencies and check for file changes
        run: |
          for file in pyproject.toml CHANGELOG.md; do
              base_name=$(basename $file .${file##*.} | tr '[:upper:]' '[:lower:]')
              if git diff --name-only HEAD^ | grep -q "^$file$"; then
                echo "${base_name}_changed=true" >> $GITHUB_ENV
              else
                echo "${base_name}_changed=false" >> $GITHUB_ENV
              fi
          done

      - name: Verify version change and CHANGELOG.md update
        if: env.pyproject_changed == 'true'
        run: |
          VERSION_BEFORE=$(git show HEAD^:pyproject.toml | python -c "import tomllib, sys; print(tomllib.loads(sys.stdin.read())['project']['version'])")
          VERSION_AFTER=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
          if [ "$VERSION_BEFORE" != "$VERSION_AFTER" ] && [ "$history_changed" == "false" ]; then
            echo "Version changed in pyproject.toml but no changes in CHANGELOG.md"
            exit 1
          elif [ "$VERSION_BEFORE" != "$VERSION_AFTER" ] && [ "$history_changed" == "true" ]; then
            echo "Version changed and CHANGELOG.md updated."
          else
            echo "Version did not change. Changes to CHANGELOG.md not required."
          fi

      - name: No changes detected
        if: env.pyproject_changed == 'false' && env.history_changed == 'false'
        run: echo "No changes detected in pyproject.toml or CHANGELOG.md. Everything is fine."