#!/bin/bash
# Main build script for Hero Vault Extension
# This script handles the complete build process in one step

set -e  # Exit on any error

# Colors for better readability
GREEN="\033[0;32m"
BLUE="\033[0;34m"
RESET="\033[0m"

echo -e "${BLUE}=== Building Hero Vault Extension ===${RESET}"

# Step 1: Build the WASM package
echo -e "${BLUE}Building WASM package...${RESET}"
cd "$(dirname "$0")/wasm_app" || exit 1
wasm-pack build --target web
echo -e "${GREEN}✓ WASM build successful!${RESET}"

# Step 2: Build the frontend extension
echo -e "${BLUE}Building frontend extension...${RESET}"
cd ../hero_vault_extension || exit 1

# Copy WASM files to the extension's public directory
echo "Copying WASM files..."
mkdir -p public/wasm
cp ../wasm_app/pkg/wasm_app* public/wasm/
cp ../wasm_app/pkg/*.d.ts public/wasm/
cp ../wasm_app/pkg/package.json public/wasm/

# Build the extension without TypeScript checking
echo "Building extension..."
export NO_TYPECHECK=true
npm run build

# Ensure the background script is properly built
echo "Building background script..."
node scripts/build-background.js
echo -e "${GREEN}✓ Frontend build successful!${RESET}"

echo -e "${GREEN}=== Build Complete ===${RESET}"
echo "Extension is ready in: $(pwd)/dist"
echo ""
echo -e "${BLUE}To load the extension in Chrome:${RESET}"
echo "1. Go to chrome://extensions/"
echo "2. Enable Developer mode (toggle in top-right)"
echo "3. Click 'Load unpacked'"
echo "4. Select the 'dist' directory: $(pwd)/dist"
