name: CI

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

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          version: "latest"

      - name: Set up Python
        run: uv python install 3.12

      - name: Install dependencies
        run: uv sync

      - name: Lint
        run: make lint

      - name: Format check
        run: make format-check

      - name: Type check
        run: make typecheck

      - name: Check imports resolve
        run: make imports-check

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

    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          version: "latest"

      - name: Set up Python ${{ matrix.python-version }}
        run: uv python install ${{ matrix.python-version }}

      - name: Install make (Windows)
        if: runner.os == 'Windows'
        run: choco install make -y

      - name: Install dependencies
        run: uv sync

      - name: Run unit tests with coverage
        run: make test-ci
        env:
          LILBEE_DATA: ${{ runner.temp }}/lilbee

      - name: Upload coverage HTML
        if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'
        uses: actions/upload-pages-artifact@v3
        with:
          path: htmlcov/

  deploy-coverage:
    needs: test
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deploy.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deploy
        uses: actions/deploy-pages@v4

  test-with-ollama:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - uses: actions/checkout@v4

      - name: Install uv
        uses: astral-sh/setup-uv@v5
        with:
          version: "latest"

      - name: Set up Python
        run: uv python install 3.12

      - name: Install dependencies
        run: uv sync

      - name: Install Ollama
        run: curl -fsSL https://ollama.com/install.sh | sh

      - name: Start Ollama and pull models
        run: |
          ollama serve &
          sleep 5
          ollama pull nomic-embed-text
          ollama pull mistral

      - name: Run full test suite (including accuracy)
        run: uv run pytest tests/ -v
        env:
          LILBEE_DATA: ${{ runner.temp }}/lilbee
          LILBEE_CHAT_MODEL: mistral
