name: Cypress E2E Tests

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

jobs:
    # Job 1: Basic E2E Tests (fast, no external dependencies)
    cypress-basic:
        name: Basic E2E Tests
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v6

            - name: Setup Node.js
              uses: actions/setup-node@v6
              with:
                  node-version: '22'
                  cache: 'npm'

            - name: Install pixi
              uses: prefix-dev/setup-pixi@v0.9.4

            - name: Install dependencies
              run: npm ci

            - name: Run Cypress (basic tests)
              uses: cypress-io/github-action@v7
              with:
                  start: pixi run dev
                  wait-on: http://127.0.0.1:7865
                  record: false
                  spec: |
                      cypress/e2e/canvas_interactions.cy.js
                      cypress/e2e/factcheck_modal.cy.js
                      cypress/e2e/help_modal.cy.js
                      cypress/e2e/keyboard_interactions.cy.js
                      cypress/e2e/new_canvas.cy.js
                      cypress/e2e/node_selection.cy.js
                      cypress/e2e/note_node.cy.js
                      cypress/e2e/settings_modal.cy.js
                      cypress/e2e/undo_redo.cy.js

            - name: Upload screenshots on failure
              uses: actions/upload-artifact@v7
              if: failure()
              with:
                  name: cypress-screenshots
                  path: cypress/screenshots

    # Job 2: AI E2E Tests (with native Ollama, slower)
    cypress-ai:
        name: AI E2E Tests (Ollama)
        runs-on: ubuntu-latest

        steps:
            - name: Checkout
              uses: actions/checkout@v6

            - name: Setup Node.js
              uses: actions/setup-node@v6
              with:
                  node-version: '22'
                  cache: 'npm'

            - name: Install pixi
              uses: prefix-dev/setup-pixi@v0.9.4

            - name: Install dependencies
              run: npm ci

            - name: Cache Ollama model weights
              id: cache-ollama-model
              uses: actions/cache@v5
              with:
                  path: ~/.ollama
                  key: ${{ runner.os }}-ollama-gemma3n-e4b
                  restore-keys: |
                      ${{ runner.os }}-ollama-gemma3n-e4b
                      ${{ runner.os }}-ollama-gemma3n-

            - name: Install Ollama
              run: |
                  curl -fsSL https://ollama.com/install.sh | sh
                  # Stop systemd service so we run Ollama as runner user and use ~/.ollama (cache path)
                  sudo systemctl stop ollama || true
                  nohup ollama serve > /tmp/ollama-serve.log 2>&1 &
                  sleep 5

            - name: Wait for Ollama to be healthy
              run: |
                  timeout 300 bash -c 'until curl -s http://localhost:11434/api/tags > /dev/null; do sleep 2; done'
                  echo "Ollama service is ready"

            - name: Check if model exists and pull if needed
              run: |
                  MODEL_EXISTS=$(curl -s http://localhost:11434/api/tags | grep -o '"name":"gemma3n:e4b"' || echo "")
                  if [ -n "$MODEL_EXISTS" ]; then
                      echo "Model already available (cache hit), skipping pull"
                  else
                      echo "Pulling gemma3n:e4b..."
                      ollama pull gemma3n:e4b
                      echo "Model pull completed"
                  fi

            - name: Run Cypress (AI tests only)
              uses: cypress-io/github-action@v7
              with:
                  start: pixi run dev
                  wait-on: http://127.0.0.1:7865
                  record: false
                  spec: cypress/e2e/**/*ai*.cy.js

            - name: Upload screenshots on failure
              uses: actions/upload-artifact@v7
              if: failure()
              with:
                  name: cypress-ai-screenshots
                  path: cypress/screenshots
