name: CI

on:
  push:
    branches: [ '**' ]  # Run on all branches
  pull_request:
    branches: [ main, develop ]

jobs:
  # Quick checks for feature branches - runs on every push
  quick-check:
    name: Quick Check (Lint + Unit Tests)
    runs-on: ubuntu-latest
    # Only run on feature branches (not main/develop)
    if: github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' && github.event_name == 'push'

    steps:
    - uses: actions/checkout@v4

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

    - name: Install uv
      uses: astral-sh/setup-uv@v7
      with:
        enable-cache: true

    - name: Install dependencies
      run: |
        uv sync --extra dev --extra observability

    - name: Run linter
      run: |
        uv run ruff check ondine/ tests/

    - name: Run formatter check
      run: |
        uv run ruff format --check ondine/ tests/

    - name: Run unit tests only
      run: |
        uv run pytest tests/unit/ -v --tb=short

  # Full test suite for main/develop/PRs
  test:
    name: Test Python ${{ matrix.python-version }} on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    # Run on main, develop, or PRs
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' || github.event_name == 'pull_request'

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ['3.10', '3.11', '3.12']

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install uv
      uses: astral-sh/setup-uv@v7
      with:
        enable-cache: true

    - name: Install dependencies
      run: |
        uv sync --extra dev --extra observability

    - name: Run linter
      run: |
        uv run ruff check ondine/ tests/ || true

    - name: Run type checker
      run: |
        uv run mypy ondine/ || true

    - name: Run unit tests
      run: |
        uv run pytest tests/unit/ -v --tb=short

    - name: Run integration tests (main branch only)
      if: github.ref == 'refs/heads/main' && matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11'
      env:
        GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
      run: |
        uv run pytest tests/integration/ -v --tb=short

    - name: Run all tests with coverage
      if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11'
      env:
        GROQ_API_KEY: ${{ secrets.GROQ_API_KEY }}
      run: |
        uv run pytest --cov=ondine --cov-report=xml --cov-report=term

    - name: Upload coverage to Codecov
      if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11'
      uses: codecov/codecov-action@v4
      with:
        file: ./coverage.xml
        fail_ci_if_error: false
        token: ${{ secrets.CODECOV_TOKEN }}

  security:
    name: Security Scan
    runs-on: ubuntu-latest
    # Only run on main/develop/PRs
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' || github.event_name == 'pull_request'

    steps:
    - uses: actions/checkout@v4

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

    - name: Install uv
      uses: astral-sh/setup-uv@v7

    - name: Install dependencies
      run: uv sync --extra dev

    - name: Run security scan
      run: |
        uv run pip-audit || true

  build:
    name: Build Package
    runs-on: ubuntu-latest
    needs: [test]
    # Only run on main/develop/PRs
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop' || github.event_name == 'pull_request'

    steps:
    - uses: actions/checkout@v4

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

    - name: Install uv
      uses: astral-sh/setup-uv@v7

    - name: Build package
      run: |
        uv build

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: dist
        path: dist/
