name: CI

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

jobs:
  lint-and-test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11", "3.13"]

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

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install -r pos/requirements.txt
          pip install -r requirements-dev.txt

      - name: Determine changed Python files
        id: changed
        run: |
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            BASE="${{ github.event.pull_request.base.sha }}"
          else
            BASE="HEAD~1"
          fi
          FILES=$(git diff --name-only --diff-filter=ACMRT "$BASE" HEAD | grep '\.py$' || true)
          echo "files=$FILES" >> "$GITHUB_OUTPUT"
          echo "Changed Python files:"
          echo "$FILES"

      - name: Lint changed files with ruff
        run: |
          FILES="${{ steps.changed.outputs.files }}"
          if [ -z "$FILES" ]; then
            echo "No Python files changed. Skipping lint."
            exit 0
          fi
          ruff check $FILES
          ruff format --check $FILES

      - name: Run console unit tests
        run: |
          python -m pytest console/tests/test_core.py console/tests/test_utils.py -v

      # Playwright E2E tests require the full stack (PostgreSQL, Redis, etc.).
      # Enable this job once a test environment is available in CI.
      # - name: Run E2E tests
      #   run: |
      #     npm ci
      #     npx playwright install --with-deps chromium
      #     npx playwright test
