name: Build WASM & Sync to Web Repo

# Trigger exactly when your auto-tagger creates a new 'v*' release
on:
  push:
    tags:
      - 'v*'

jobs:
  build-and-sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout MoltenDB Core (Rust)
        uses: actions/checkout@v4
        with:
          path: core-repo

      - name: Checkout Web Monorepo (TS/JS)
        uses: actions/checkout@v4
        with:
          repository: maximilian27/moltendb-web
          path: web-repo
          token: ${{ secrets.REPO_PAT }} # CRITICAL: Authorizes the push to the second repo

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

      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: Build WASM
        working-directory: ./core-repo
        run: wasm-pack build --target web

      - name: Surgical Artifact Sync
        run: |
          # Define exact paths to prevent typos
          SRC_PKG="core-repo/pkg"
          DEST_CORE="web-repo/packages/core"
          DEST_WASM="$DEST_CORE/src/wasm"
          
          echo "Syncing artifacts to $DEST_WASM..."
          
          # 1. Ensure the dist directory exists
          mkdir -p "$DEST_WASM"
          
          # 2. Copy exactly the 4 code artifacts into /packages/core/dist/
          cp "$SRC_PKG/moltendb.d.ts" "$DEST_WASM/"
          cp "$SRC_PKG/moltendb.js" "$DEST_WASM/"
          cp "$SRC_PKG/moltendb_bg.wasm" "$DEST_WASM/"
          cp "$SRC_PKG/moltendb_bg.wasm.d.ts" "$DEST_WASM/"
          

      - name: Commit and Push to Web Monorepo
        working-directory: ./web-repo
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          
          git add .
          
          # Prevent the workflow from crashing if the files haven't actually changed
          if git diff --staged --quiet; then
            echo "No WASM changes detected. Skipping push."
          else
            git commit -m "chore: sync WASM engine artifacts & bump version to ${{ github.ref_name }}"
          
            git push origin develop 
          fi