#!/usr/bin/env bash
# 周辺基盤（compose）とアプリ2つを起動する。ログは .run/ に出る。
set -euo pipefail
cd "$(dirname "$0")/.."
mkdir -p .run
podman compose -f infra/compose.yaml up -d
[ -f agent-tools/target/quarkus-app/quarkus-run.jar ] || mvn -q -DskipTests package

echo "waiting for Keycloak ..."
until curl -sf http://localhost:8180/realms/banking/.well-known/openid-configuration >/dev/null; do sleep 2; done

nohup java -jar core-banking-mock/target/quarkus-app/quarkus-run.jar > .run/core.log 2>&1 &
echo $! > .run/core.pid
nohup java -jar agent-tools/target/quarkus-app/quarkus-run.jar > .run/tools.log 2>&1 &
echo $! > .run/tools.pid

for url in http://localhost:8081/admin/faults http://localhost:8080/q/health; do
  until curl -sf "$url" >/dev/null 2>&1; do sleep 1; done
done
echo "ready:"
echo "  agent-tools     http://localhost:8080  (MCP: /mcp/read, /mcp/write)"
echo "  core (mock)     http://localhost:8081"
echo "  Keycloak        http://localhost:8180  (admin/admin)"
echo "  Tempo（トレース） http://localhost:3000/explore （Grafana のデータソース Tempo）"
echo "  Grafana         http://localhost:3000/d/agent-safe-connect"
