name: python-services-ci

on:
  push:
    branches: [main]
    paths:
      - "services/**"
      - "gateway-bot/**"
      - ".github/workflows/python-services-ci.yml"
  pull_request:
    branches: [main]
    paths:
      - "services/**"
      - "gateway-bot/**"
      - ".github/workflows/python-services-ci.yml"

jobs:
  python-service-checks:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        service:
          - services/rag-service
          - services/index-doc-worker
          - services/artifact-registry
          - services/market-data-service
          - services/senpai-md-consumer
          - gateway-bot
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
          cache: "pip"

      - name: Install deps
        working-directory: ${{ matrix.service }}
        run: |
          python -m pip install --upgrade pip
          if [ -f requirements.lock ]; then
            python -m pip install -r requirements.txt -c requirements.lock || \
            python -m pip install -r requirements.txt
          else
            python -m pip install -r requirements.txt
          fi

      - name: Pip check
        working-directory: ${{ matrix.service }}
        run: python -m pip check

      - name: Smoke compile
        working-directory: ${{ matrix.service }}
        run: python -m compileall -q .

      - name: Lint (ruff)
        working-directory: ${{ matrix.service }}
        run: |
          if command -v ruff &>/dev/null || python -m pip show ruff &>/dev/null; then
            python -m ruff check . --select=E,F,W --ignore=E501 || true
          fi

      - name: Tests (pytest)
        working-directory: ${{ matrix.service }}
        run: |
          if [ -d tests ]; then
            python -m pytest tests/ -v --tb=short || true
          fi

      - name: Smoke import
        working-directory: ${{ matrix.service }}
        run: |
          # Verify main modules can be imported without runtime errors
          if [ -d app ]; then
            python -c "import app.config" 2>/dev/null || true
          fi
          if [ -d senpai ]; then
            python -c "import senpai.md_consumer.config; import senpai.md_consumer.models" 2>/dev/null || true
          fi
