name: Comprehensive Tests

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

permissions:
  pull-requests: write
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    
    services:
      chroma:
        image: chromadb/chroma:latest
        ports:
          - 8000:8000

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0 # Needed for changed-files detection

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          cache: 'pip'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e .
          pip install pytest pytest-cov

      - name: Cache Models
        id: cache-models
        uses: actions/cache@v4
        with:
          path: ~/.local/share/candlekeep/models
          key: ${{ runner.os }}-models-${{ hashFiles('src/candlekeep/config.py') }}

      - name: Setup Candlekeep (Download Models)
        if: steps.cache-models.outputs.cache-hit != 'true'
        run: |
          mkdir -p ~/.local/share/candlekeep
          python -m spacy download en_core_web_sm
          python << 'EOF'
          import os
          from pathlib import Path
          from sentence_transformers import SentenceTransformer, CrossEncoder
          
          models_dir = Path.home() / ".local" / "share" / "candlekeep" / "models"
          models_dir.mkdir(parents=True, exist_ok=True)
          
          print(f"Downloading Bi-Encoder to {models_dir}")
          SentenceTransformer("BAAI/bge-small-en-v1.5", cache_folder=str(models_dir))
          
          print(f"Downloading Cross-Encoder to {models_dir}")
          from sentence_transformers import CrossEncoder
          CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2", cache_folder=str(models_dir))
          EOF

      - name: Run Unit Tests and Sanity Benchmarks
        env:
          XDG_DATA_HOME: /home/runner/.local/share
          CANDLEKEEP_DEVICE: cpu
          CHROMA_URL: http://localhost:8000
        run: |
          export PYTHONPATH=$PYTHONPATH:$(pwd)/src
          # Run logic tests and quality benchmarks
          pytest -m "unit or benchmark" -v -s | tee pytest-output.txt

      - name: Check for Integration Test Requirement
        id: changed-files
        uses: tj-actions/changed-files@v44
        with:
          files: |
            src/candlekeep/database/**
            src/candlekeep/config.py
            tests/test_vector_store.py
            tests/test_search.py
            tests/test_multi_agent_integration.py

      - name: Run Integration Tests
        if: steps.changed-files.outputs.any_changed == 'true'
        env:
          XDG_DATA_HOME: /home/runner/.local/share
          CANDLEKEEP_DEVICE: cpu
          CHROMA_URL: http://localhost:8000
        run: |
          export PYTHONPATH=$PYTHONPATH:$(pwd)/src
          # Run tests that require a live ChromaDB connection
          pytest -m "integration" -v -s >> pytest-output.txt

      - name: Prepare PR Comment
        if: github.event_name == 'pull_request' && always()
        run: |
          echo "## 🛡️ CI Test Results" > pr_comment.md
          echo "" >> pr_comment.md
          
          if [ -f pytest-output.txt ]; then
            PASSED=$(grep -c "PASSED" pytest-output.txt || true)
            FAILED=$(grep -c "FAILED" pytest-output.txt || true)
            SKIPPED=$(grep -c "SKIPPED" pytest-output.txt || true)
            
            echo "### Summary" >> pr_comment.md
            echo "- ✅ **Passed:** $PASSED" >> pr_comment.md
            echo "- ❌ **Failed:** $FAILED" >> pr_comment.md
            echo "- ⏭️ **Skipped:** $SKIPPED" >> pr_comment.md
            echo "" >> pr_comment.md
            
            # Path-specific comparisons consolidated into a single table
            # Order: Simple -> Hybrid -> Precise
            python3 scripts/compare_benchmarks.py \
              "Simple Path (Arcane Recall)" tests/results/archive/router_simple_benchmark.json tests/results/router_simple_benchmark.json \
              "Hybrid Path (BM25 + Vector)" tests/results/archive/router_hybrid_benchmark.json tests/results/router_hybrid_benchmark.json \
              "Precise Path (Reranker)" tests/results/archive/router_precise_benchmark.json tests/results/router_precise_benchmark.json \
              >> pr_comment.md
          else
            echo "⚠️ No test output found." >> pr_comment.md
          fi
          
          echo "" >> pr_comment.md
          echo "*Updated at: $(date -u)*" >> pr_comment.md

      - name: Post PR Comment
        uses: marocchino/sticky-pull-request-comment@v2
        if: github.event_name == 'pull_request' && always()
        with:
          header: test-results
          path: pr_comment.md
          recreate: true
          only_if_last_comment_is_not_from_bot: true
