name: CI

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

jobs:
  quality:
    name: Quality (${{ matrix.python-version }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.10", "3.11", "3.12"]

    steps:
      - uses: actions/checkout@v6

      - name: Install uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: ${{ matrix.python-version }}

      - uses: pnpm/action-setup@v5
        with:
          version: 10

      - uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: pnpm
          cache-dependency-path: js/pnpm-lock.yaml

      - name: Install Python dependencies
        run: uv sync --frozen --dev

      - name: Install JS dependencies and build
        run: |
          cd js
          pnpm install
          pnpm build

      - name: JS lint (eslint)
        run: cd js && pnpm lint

      - name: JS test (vitest)
        run: cd js && pnpm test

      - name: Lint (ruff check)
        run: uv run ruff check .

      - name: Format check (ruff format)
        run: uv run ruff format --check .

      - name: Type check (mypy)
        run: uv run mypy src/lizyml_widget/

      - name: Test (pytest)
        run: uv run pytest --cov=lizyml_widget --cov-report=term-missing --cov-fail-under=80 -q

  distribution:
    name: Distribution check
    runs-on: ubuntu-latest

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

      - name: Install uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: "3.12"

      - uses: pnpm/action-setup@v5
        with:
          version: 10

      - uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: pnpm
          cache-dependency-path: js/pnpm-lock.yaml

      - name: Build JS
        run: |
          cd js
          pnpm install
          pnpm build

      - name: Install build tools
        run: uv sync --frozen --dev

      - name: Build sdist and wheel
        run: uv build

      - name: Check distribution
        run: uv run twine check dist/*

      - name: Smoke test installation from wheel
        run: |
          pip install dist/*.whl --quiet
          python -c "from lizyml_widget import LizyWidget; print('import ok:', LizyWidget)"
