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

cd "$(dirname "$0")"

# ── Build the React UI ──────────────────────────────────────────────
echo "==> Building UI..."
(cd ui && npm install --silent && npm run build)
echo "    UI built -> peon_mcp/static/"

# ── Clean previous builds ───────────────────────────────────────────
echo "==> Cleaning dist/..."
rm -rf dist/

# ── Build the Python package ────────────────────────────────────────
echo "==> Building Python package..."
uv build
echo "    Package built -> dist/"

# ── Publish ─────────────────────────────────────────────────────────
TARGET="${1:-pypi}"

if [ "$TARGET" = "testpypi" ]; then
    echo "==> Publishing to TestPyPI..."
    uv publish --publish-url https://test.pypi.org/legacy/ --token $UV_PUBLISH_TOKEN
else
    echo "==> Publishing to PyPI..."
    uv publish --token $UV_PUBLISH_TOKEN
fi

echo "==> Done!"
