name: CI

on:
  push:
    branches: [main, 'v*']
  pull_request:
    branches: [main]

jobs:
  lint-and-test:
    name: Lint & Test
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - uses: mlugg/setup-zig@v2
        with:
          version: 0.15.2

      - name: Configure git for tests
        run: |
          git config --global user.name "CI"
          git config --global user.email "ci@example.com"
          git config --global init.defaultBranch main

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Generate skill registry
        run: bun run generate

      - name: Lint
        run: bun run lint

      - name: Format check
        run: bun run format:check

      - name: Checkout openpencil-skill
        uses: actions/checkout@v4
        with:
          repository: zseven-w/openpencil-skill
          path: external/openpencil-skill

      - name: Bundle CLI skill
        env:
          SKILL_ROOT: ${{ github.workspace }}/external/openpencil-skill
        run: bun run cli:bundle-skill

      - name: Type check
        run: npx tsc --noEmit

      - name: Run tests
        run: bun --bun run test

  build-web:
    name: Build Web
    runs-on: ubuntu-latest
    timeout-minutes: 10
    needs: lint-and-test

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - uses: mlugg/setup-zig@v2
        with:
          version: 0.15.2

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Build
        run: bun --bun run build

      - name: Checkout openpencil-skill
        uses: actions/checkout@v4
        with:
          repository: zseven-w/openpencil-skill
          path: external/openpencil-skill

      - name: Build CLI
        env:
          SKILL_ROOT: ${{ github.workspace }}/external/openpencil-skill
        run: bun run cli:compile

      - name: Upload web build artifact
        uses: actions/upload-artifact@v4
        with:
          name: web-build
          path: out/web/
          retention-days: 7
