#!/bin/bash
# Production WASM Build Script
# This script compiles the Rust WASM module with optimizations

set -e

echo "🔨 Building Production WASM Module..."
echo ""

# Check for Rust
if ! command -v rustc &> /dev/null; then
    echo "❌ Rust not found!"
    echo "Install: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
    exit 1
fi

echo "✅ Rust found: $(rustc --version)"

# Check for wasm-pack
if ! command -v wasm-pack &> /dev/null; then
    echo "📦 Installing wasm-pack..."
    cargo install wasm-pack
fi

echo "✅ wasm-pack found: $(wasm-pack --version)"

# Add WASM target
echo "🎯 Adding WASM target..."
rustup target add wasm32-unknown-unknown

# SIMD128 is configured in .cargo/config.toml (authoritative source)

# Build with wasm-pack
echo "📦 Building WASM (release mode, SIMD128 enabled via .cargo/config.toml)..."
wasm-pack build \
  --target web \
  --out-dir pkg \
  --release \
  --scope fikoyt

echo "✅ WASM build complete!"

# Check output
echo ""
echo "📊 Build artifacts:"
ls -lh pkg/ | grep -E "fikoyt_wasm"

# Copy to public/assets
echo ""
echo "📁 Copying to public/assets/fikoyt-wasm..."
mkdir -p ../public/assets/fikoyt-wasm
cp -v pkg/fikoyt_wasm* ../public/assets/fikoyt-wasm/
cp -v pkg/package.json ../public/assets/fikoyt-wasm/

echo ""
echo "✅ Production WASM ready!"
echo "📊 Location: public/assets/fikoyt-wasm/"
echo ""
echo "🚀 Next step: npm run serve"
