name: CI

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

permissions:
  contents: read

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ["3.10", "3.11", "3.12", "3.13"]

    steps:
    - uses: actions/checkout@v6
      with:
        fetch-depth: 0

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v6
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'pip'

    - name: Install dependencies
      run: python -m pip install --upgrade pip build pytest pytest-cov

    - name: Build wheel
      run: python -m build

    - name: Install from wheel
      shell: bash
      run: python -m pip install dist/*.whl

    - name: Smoke test — rekall demo
      run: rekall demo --quiet

    - name: Run unit tests with coverage
      run: pytest tests/ -v --tb=short --cov=rekall --cov-report=xml --cov-report=term-missing

    - name: Dogfooding - Validate State and MCP
      if: success()
      run: |
        rekall validate ./project-state --strict
        rekall validate --mcp --server-cmd "python -m rekall.server.mcp_server"

    - name: Dogfooding - Update Timeline
      if: success()
      run: |
        rekall timeline add --summary "CI Passed: ${{ github.sha }} in ${{ github.workflow }}" --store-dir ./project-state --idempotency-key "ci-${{ github.sha }}" --actor "github_actions"

    - name: Upload coverage report
      if: success() && matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
      uses: actions/upload-artifact@v6
      with:
        name: coverage-report
        path: coverage.xml
        retention-days: 7

    - name: Upload test logs on failure
      if: failure()
      uses: actions/upload-artifact@v6
      with:
        name: test-logs-${{ matrix.os }}-py${{ matrix.python-version }}
        path: |
          test_out.txt
          test_tmp/
        retention-days: 7
