export CONTAINER_NAME=ai_scheduler
export NETWORK_NAME=ai_scheduler_network



MODE="$1"
if [ "$MODE" != "dev" ] && [ "$MODE" != "prod" ]; then
  echo "Usage: $0 dev|prod"
  exit 1
fi

CONFIG_FILE="config/config.yaml"
BRANCH="master"
BOT="All"
CRON="start"
if [ "$MODE" = "dev" ]; then
  CONFIG_FILE="config/config_test.yaml"
  BOT="zltx"
  BRANCH="test"
  CRON="close"
fi

git fetch origin
git checkout "$BRANCH"
git pull origin "$BRANCH"

docker build -t ${CONTAINER_NAME}  .
docker stop  ${CONTAINER_NAME}
docker rm -f ${CONTAINER_NAME}

# 依赖服务绑定同一网络，以便相互通信
docker network create "${NETWORK_NAME}" 2>/dev/null || true
docker network connect "${NETWORK_NAME}" excel2pic 2>/dev/null || true
docker network connect "${NETWORK_NAME}" WeKnora-app 2>/dev/null || true

docker run -itd \
  --name "${CONTAINER_NAME}" \
  --restart=always \
  --add-host=host.docker.internal:host-gateway \
  --network="${NETWORK_NAME}" \
  -e "OLLAMA_BASE_URL=${OLLAMA_BASE_URL:-http://host.docker.internal:11434}" \
  -e "MODE=${MODE}" \
  -p 8090:8090 \
  -v ./cache:/app/cache \
  -v ./tmpl:/app/tmpl \
  -v ./go.mod:/app/go.mod \
  "${CONTAINER_NAME}" ./server \
  --config "./${CONFIG_FILE}"  --bot "${BOT}" --cron "${CRON}"


docker logs -f ${CONTAINER_NAME}