name: CI

on:
  push:
    branches: [ master, main, develop ]
    paths:
      - 'packages/**'
      - 'package.json'
      - 'pnpm-lock.yaml'
      - 'tsconfig.json'
      - 'jest.config.*'
      - '.github/workflows/ci.yml'
  pull_request:
    branches: [ master, main, develop ]
    paths:
      - 'packages/**'
      - 'package.json'
      - 'pnpm-lock.yaml'
      - 'tsconfig.json'
      - 'jest.config.*'
      - '.github/workflows/ci.yml'

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Install pnpm
      uses: pnpm/action-setup@v2
      with:
        version: 8

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20.x'
        cache: 'pnpm'

    - name: Install dependencies
      run: pnpm install

    - name: Install Rust stable
      uses: dtolnay/rust-toolchain@stable

    - name: Install wasm-pack
      run: cargo install wasm-pack

    - name: Build core package first
      run: pnpm run build:core

    - name: Build engine WASM package
      run: |
        cd packages/engine
        pnpm run build

    - name: Copy WASM files to ecs-engine-bindgen
      run: |
        mkdir -p packages/ecs-engine-bindgen/src/wasm
        cp packages/engine/pkg/es_engine.js packages/ecs-engine-bindgen/src/wasm/
        cp packages/engine/pkg/es_engine.d.ts packages/ecs-engine-bindgen/src/wasm/
        cp packages/engine/pkg/es_engine_bg.wasm packages/ecs-engine-bindgen/src/wasm/
        cp packages/engine/pkg/es_engine_bg.wasm.d.ts packages/ecs-engine-bindgen/src/wasm/

    - name: Build dependent packages for type declarations
      run: |
        cd packages/platform-common && pnpm run build
        cd ../asset-system && pnpm run build
        cd ../components && pnpm run build
        cd ../editor-core && pnpm run build
        cd ../ui && pnpm run build
        cd ../editor-runtime && pnpm run build
        cd ../behavior-tree && pnpm run build
        cd ../tilemap && pnpm run build
        cd ../physics-rapier2d && pnpm run build
        cd ../node-editor && pnpm run build
        cd ../blueprint && pnpm run build

    - name: Build ecs-engine-bindgen
      run: |
        cd packages/ecs-engine-bindgen && pnpm run build

    - name: Type check
      run: pnpm run type-check

    - name: Lint check
      run: pnpm run lint

    - name: Run tests with coverage
      run: pnpm run test:ci

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v4
      continue-on-error: true
      with:
        file: ./coverage/lcov.info
        flags: unittests
        name: codecov-umbrella
        fail_ci_if_error: false

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

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Install pnpm
      uses: pnpm/action-setup@v2
      with:
        version: 8

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20.x'
        cache: 'pnpm'

    - name: Install dependencies
      run: pnpm install

    - name: Build project
      run: pnpm run build

    - name: Build npm package
      run: pnpm run build:npm

    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: build-artifacts
        path: |
          bin/
          dist/
        retention-days: 7
