#!/bin/bash
set -e

# This script builds the standalone binary for macOS natively.
# It requires Python 3.10+ to be installed on your Mac.

echo "🏗️ Setting up macOS native builder..."
cd "$(dirname "$0")/../.."

echo "🔨 Creating ephemeral virtual environment..."
python3 -m venv .build-venv
source .build-venv/bin/activate

echo "📦 Installing requirements and PyInstaller..."
pip install --upgrade pip
pip install pyinstaller
pip install -r agent-node/requirements.txt

echo "🔨 Building macOS Binary (darwin_arm64/amd64)..."
pyinstaller \
    --name cortex-agent \
    --onefile \
    --clean \
    --strip \
    --add-data "skills:skills" \
    --paths agent-node \
    agent-node/src/agent_node/main.py

echo "✅ Build complete! Moving binary to agent-node/dist/darwin_$(uname -m)/"
mkdir -p "agent-node/dist/darwin_$(uname -m)"
mv dist/cortex-agent "agent-node/dist/darwin_$(uname -m)/cortex-agent"
chmod +x "agent-node/dist/darwin_$(uname -m)/cortex-agent"

echo "🧹 Cleaning up build artifacts..."
rm -rf build dist cortex-agent.spec
deactivate
rm -rf .build-venv

echo ""
echo "📱 Your native macOS binary is ready at: agent-node/dist/darwin_$(uname -m)/cortex-agent"
echo "If you want the production server to serve this binary, simply SCP it to your server:"
echo "scp -r agent-node/dist/darwin_$(uname -m) axieyangb@192.168.68.113:/home/coder/project/cortex-hub/agent-node/dist/"
