name: CI

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

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ['3.13', '3.14']

    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
      run: pip install uv

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

    - name: Run linter
      run: uv run ruff check .

    - name: Run formatter check
      run: uv run ruff format --check .

    - name: Run type checker
      run: uv run mypy src

    - name: Run security scan
      run: uv run bandit -c pyproject.toml -r src/

    - name: Run tests
      run: uv run pytest --cov=tablesleuth --cov-report=xml --cov-report=term

    - name: Upload coverage to Codecov
      if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.13'
      uses: codecov/codecov-action@v4
      with:
        file: ./coverage.xml
        fail_ci_if_error: false

  build:
    runs-on: ubuntu-latest
    needs: test

    steps:
    - uses: actions/checkout@v4

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

    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'
        cache: 'npm'
        cache-dependency-path: web-ui/package-lock.json

    - name: Install uv
      run: pip install uv

    - name: Install Node.js dependencies
      run: cd web-ui && npm ci

    - name: Build frontend
      run: cd web-ui && npm run build

    - name: Bundle frontend into package
      run: |
        rm -rf src/tablesleuth/web
        cp -r web-ui/out src/tablesleuth/web

    - name: Build Python package
      run: uv build

    - name: Check package
      run: |
        pip install twine
        twine check dist/*

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