#!/usr/bin/env bash
# Copy HYBX OMNL–related paths from smom-dbis-138 → smom-dbis-138-publish (adjust PUBLISH_ROOT if needed).
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
PUBLISH_ROOT="${PUBLISH_ROOT:-$ROOT/../smom-dbis-138-publish}"
if [[ ! -d "$PUBLISH_ROOT" ]]; then
  echo "error: PUBLISH_ROOT not found: $PUBLISH_ROOT" >&2
  exit 1
fi

rsync -a "$ROOT/contracts/hybx-omnl/" "$PUBLISH_ROOT/contracts/hybx-omnl/"
rsync -a "$ROOT/script/hybx-omnl/" "$PUBLISH_ROOT/script/hybx-omnl/"
rsync -a "$ROOT/test/hybx-omnl/" "$PUBLISH_ROOT/test/hybx-omnl/"
rsync -a "$ROOT/docs/hybx-omnl/" "$PUBLISH_ROOT/docs/hybx-omnl/"
for f in hybx-omnl-policy.json hybx-omnl-cross-chain-lines.json omnl-ipsas-gl-registry.json omnl-journal-matrix.json deployment-omnl.example.env; do
  [[ -f "$ROOT/config/$f" ]] && cp -f "$ROOT/config/$f" "$PUBLISH_ROOT/config/$f"
done
rsync -a "$ROOT/scripts/hybx-omnl/" "$PUBLISH_ROOT/scripts/hybx-omnl/"
rsync -a "$ROOT/scripts/forge/scope.sh" "$PUBLISH_ROOT/scripts/forge/scope.sh"
mkdir -p "$PUBLISH_ROOT/.github/workflows"
for w in hybx-omnl-ts.yml omnl-reconcile.yml; do
  [[ -f "$ROOT/.github/workflows/$w" ]] && cp -f "$ROOT/.github/workflows/$w" "$PUBLISH_ROOT/.github/workflows/$w"
done

TA="$ROOT/services/token-aggregation"
PTA="$PUBLISH_ROOT/services/token-aggregation"
for f in omnl-webhooks.ts omnl-ipsas-gl.ts omnl-journal-matrix.ts omnl-compliance.ts omnl-policy-math.ts omnl-reconcile-anchor.ts omnl-integration-status.ts omnl-api-catalog.ts; do
  cp -f "$TA/src/services/$f" "$PTA/src/services/$f"
done
cp -f "$TA/src/services/omnl-reconcile-anchor.test.ts" "$PTA/src/services/omnl-reconcile-anchor.test.ts" 2>/dev/null || true
cp -f "$TA/src/api/routes/omnl.ts" "$PTA/src/api/routes/omnl.ts"
cp -f "$TA/src/api/routes/omnl-ipsas.ts" "$PTA/src/api/routes/omnl-ipsas.ts"
cp -f "$TA/src/indexer/omnl-event-poller.ts" "$PTA/src/indexer/omnl-event-poller.ts"
cp -f "$TA/src/indexer/omnl-poller-state.ts" "$PTA/src/indexer/omnl-poller-state.ts"
cp -f "$TA/src/api/server.ts" "$PTA/src/api/server.ts"
cp -f "$TA/src/api/middleware/rate-limit.ts" "$PTA/src/api/middleware/rate-limit.ts"
mkdir -p "$PTA/src/api/middleware"
cp -f "$TA/src/api/middleware/omnl-guards.ts" "$PTA/src/api/middleware/omnl-guards.ts"
cp -f "$TA/src/api/middleware/omnl-guards.test.ts" "$PTA/src/api/middleware/omnl-guards.test.ts" 2>/dev/null || true
cp -f "$TA/src/services/omnl-webhooks.test.ts" "$PTA/src/services/omnl-webhooks.test.ts" 2>/dev/null || true
mkdir -p "$PTA/src/resources"
cp -f "$TA/src/resources/omnl-openapi.json" "$PTA/src/resources/omnl-openapi.json"
cp -f "$TA/public/omnl-dashboard.html" "$PTA/public/omnl-dashboard.html"
cp -f "$TA/package.json" "$PTA/package.json"
cp -f "$ROOT/docs/deployment/DEPLOYMENT_INDEX.md" "$PUBLISH_ROOT/docs/deployment/DEPLOYMENT_INDEX.md" 2>/dev/null || true
cp -f "$ROOT/docs/MASTER_DOCUMENTATION_INDEX.md" "$PUBLISH_ROOT/docs/MASTER_DOCUMENTATION_INDEX.md" 2>/dev/null || true
cp -f "$ROOT/README.md" "$PUBLISH_ROOT/README.md" 2>/dev/null || true

# Publish verify-deployment stays lightweight (no Forge)
cat > "$PUBLISH_ROOT/scripts/hybx-omnl/verify-deployment.sh" <<'EOS'
#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT"
echo "== validate cross-chain config =="
node scripts/hybx-omnl/validate-cross-chain-config.mjs
cd services/token-aggregation
echo "== token-aggregation tsc =="
pnpm exec tsc --noEmit
echo "== omnl reconcile report =="
node scripts/omnl-reconcile-report.mjs >/dev/null
echo "verify-deployment (publish): OK"
EOS
chmod +x "$PUBLISH_ROOT/scripts/hybx-omnl/verify-deployment.sh"

echo "sync-to-publish: OK → $PUBLISH_ROOT"
