#!/usr/bin/env bash
set -euo pipefail

# Build napi native addon for all supported platforms.
# Run from the SDK root: ./npm/scripts/build-platforms.sh
#
# For CI, each platform builds on its own runner.
# Locally, only the current platform is built.

SDK_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
NPM_DIR="$SDK_ROOT/npm"

# Platform package template
create_platform_package() {
  local name="$1"
  local os="$2"
  local cpu="$3"
  local dir="$NPM_DIR/platforms/$name"

  mkdir -p "$dir"

  cat > "$dir/package.json" << EOF
{
  "name": "@lastid/$name",
  "version": "0.2.0",
  "description": "LastID SDK native binding for $os-$cpu",
  "license": "MIT OR Apache-2.0",
  "repository": {
    "type": "git",
    "url": "https://github.com/GetTrustedApp/lastid-rust-sdk"
  },
  "os": ["$os"],
  "cpu": ["$cpu"],
  "main": "lastid_sdk.node",
  "files": ["lastid_sdk.node"]
}
EOF
}

# Create platform package dirs
create_platform_package "lastid-sdk-darwin-arm64" "darwin" "arm64"
create_platform_package "lastid-sdk-darwin-x64" "darwin" "x64"
create_platform_package "lastid-sdk-linux-x64-gnu" "linux" "x64"
create_platform_package "lastid-sdk-linux-arm64-gnu" "linux" "arm64"
create_platform_package "lastid-sdk-win32-x64-msvc" "win32" "x64"

echo "Platform package dirs created."

# Build for current platform
echo "Building native addon for current platform..."
cd "$SDK_ROOT"

# napi-rs outputs the .node file to the crate's target dir
# We use cdylib crate-type which is already set in Cargo.toml
cargo build --release --features napi

# Find the built .node / .dylib / .so
# napi-rs with cdylib produces a shared library that IS the .node file
case "$(uname -s)-$(uname -m)" in
  Darwin-arm64)
    PLATFORM_DIR="$NPM_DIR/platforms/lastid-sdk-darwin-arm64"
    SRC="$SDK_ROOT/target/release/liblastid_sdk.dylib"
    ;;
  Darwin-x86_64)
    PLATFORM_DIR="$NPM_DIR/platforms/lastid-sdk-darwin-x64"
    SRC="$SDK_ROOT/target/release/liblastid_sdk.dylib"
    ;;
  Linux-x86_64)
    PLATFORM_DIR="$NPM_DIR/platforms/lastid-sdk-linux-x64-gnu"
    SRC="$SDK_ROOT/target/release/liblastid_sdk.so"
    ;;
  Linux-aarch64)
    PLATFORM_DIR="$NPM_DIR/platforms/lastid-sdk-linux-arm64-gnu"
    SRC="$SDK_ROOT/target/release/liblastid_sdk.so"
    ;;
  *)
    echo "Unsupported platform: $(uname -s)-$(uname -m)"
    exit 1
    ;;
esac

if [ -f "$SRC" ]; then
  cp "$SRC" "$PLATFORM_DIR/lastid_sdk.node"
  echo "Copied native addon to $PLATFORM_DIR/lastid_sdk.node"
  ls -lh "$PLATFORM_DIR/lastid_sdk.node"
else
  echo "ERROR: Expected $SRC but file not found."
  echo "Contents of target/release/:"
  ls "$SDK_ROOT/target/release/liblastid_sdk"* 2>/dev/null || echo "  (no liblastid_sdk files)"
  exit 1
fi

# Copy WASM files
echo "Copying WASM files..."
mkdir -p "$NPM_DIR/wasm"
cp "$SDK_ROOT/pkg/lastid_sdk_bg.wasm" "$NPM_DIR/wasm/" 2>/dev/null || echo "  WASM not built yet (run wasm-pack first)"
cp "$SDK_ROOT/pkg/lastid_sdk.js" "$NPM_DIR/wasm/" 2>/dev/null || true
cp "$SDK_ROOT/pkg/lastid_sdk.d.ts" "$NPM_DIR/wasm/" 2>/dev/null || true

echo ""
echo "Build complete. Package contents:"
find "$NPM_DIR" -name "*.node" -o -name "*.wasm" | head -20
