name: ci

# On PRs this is invoked by ci-gate.yml (the single required check);
# push-to-main runs remain standalone.
on:
  push:
    branches: [main]
  workflow_call:

jobs:
  changelog_check:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Require CHANGELOG entry when Python SDK version changes
        env:
          BASE_SHA: ${{ github.event.pull_request.base.sha }}
          HEAD_SHA: ${{ github.event.pull_request.head.sha }}
        run: |
          set -euo pipefail

          extract_version() {
            python3 -c "import sys, re; m = re.search(r'^\s*version\s*=\s*\"([^\"]+)\"', sys.stdin.read(), re.M); print(m.group(1) if m else '')"
          }

          base_version=$(git show "$BASE_SHA:pyproject.toml" 2>/dev/null | extract_version || echo "")
          head_version=$(extract_version < pyproject.toml)

          echo "Base version: ${base_version:-<unknown>}"
          echo "Head version: $head_version"

          if [ -z "$base_version" ] || [ "$base_version" = "$head_version" ]; then
            echo "pyproject.toml version unchanged — no CHANGELOG entry required."
            exit 0
          fi

          echo "Detected version bump ${base_version} -> ${head_version}. Checking docs/changelog/sdk.mdx…"

          if git diff --name-only "$BASE_SHA" "$HEAD_SHA" -- docs/changelog/sdk.mdx | grep -q .; then
            echo "Changelog update present in docs/changelog/sdk.mdx ✅"
          else
            echo "::error file=pyproject.toml::pyproject.toml version changed from ${base_version} to ${head_version} but docs/changelog/sdk.mdx was not updated in this PR. Add a new <Update> entry under the Python tab for v${head_version}."
            exit 1
          fi

  check_changes:
    runs-on: ubuntu-latest
    outputs:
      mem0_changed: ${{ steps.filter.outputs.mem0 }}
    steps:
    - uses: actions/checkout@v4
    - uses: dorny/paths-filter@v2
      id: filter
      with:
        filters: |
          mem0:
            - 'mem0/**'
            - 'tests/**'
            - '.github/workflows/ci.yml'
            - 'pyproject.toml'

  build_mem0:
    needs: check_changes
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12"]
    steps:
      - name: Skip — no relevant changes
        if: needs.check_changes.outputs.mem0_changed != 'true'
        run: echo "No changes in mem0/, tests/, pyproject.toml, or ci.yml — skipping"
      - uses: actions/checkout@v4
        if: needs.check_changes.outputs.mem0_changed == 'true'
      - name: Set up Python ${{ matrix.python-version }}
        if: needs.check_changes.outputs.mem0_changed == 'true'
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Clean up disk space
        if: needs.check_changes.outputs.mem0_changed == 'true'
        run: |
          df -h
          sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL
          sudo docker image prune --all --force
          sudo docker builder prune -a
          df -h
      - name: Install Hatch
        if: needs.check_changes.outputs.mem0_changed == 'true'
        run: pip install hatch
      - name: Load cached venv
        if: needs.check_changes.outputs.mem0_changed == 'true'
        id: cached-hatch-dependencies
        uses: actions/cache@v3
        with:
          path: .venv
          key: venv-mem0-${{ runner.os }}-${{ hashFiles('**/pyproject.toml') }}
      - name: Install GEOS Libraries
        if: needs.check_changes.outputs.mem0_changed == 'true'
        run: sudo apt-get update && sudo apt-get install -y libgeos-dev
      - name: Install dependencies
        if: needs.check_changes.outputs.mem0_changed == 'true' && steps.cached-hatch-dependencies.outputs.cache-hit != 'true'
        run: |
          pip install --upgrade pip
          pip install -e ".[test,graph,vector_stores,llms,extras]"
          pip install ruff
      - name: Run Linting
        if: needs.check_changes.outputs.mem0_changed == 'true'
        run: make lint
      - name: Run tests and generate coverage report
        if: needs.check_changes.outputs.mem0_changed == 'true'
        run: make test
