#!/usr/bin/env bash

set -euo pipefail

# One-click setup for ritmex-bot (Linux/macOS)
# - Ensures Bun is installed
# - Installs dependencies
# - Prompts for API credentials
# - Generates .env
# - Prepares to start the bot (does not auto-run)

main() {
  require_unix
  ensure_repo
  referral_notice
  ensure_bun
  install_deps
  prompt_env
  write_env
  echo
  echo "✅ 安装完成！"
  echo "项目目录：$PROJECT_DIR"
  echo "请在终端中依次输入以下命令启动："
  echo
  echo "  cd ritmex-bot"
  echo "  bun start"
  echo
  echo "提示：启动前可在项目目录内检查或编辑 .env 文件。"
}

require_unix() {
  case "$(uname -s)" in
    Linux|Darwin) ;;
    *) echo "This script supports only Linux and macOS." >&2; exit 1 ;;
  esac
}

ensure_bun() {
  if command -v bun >/dev/null 2>&1; then
    echo "✔ Bun found: $(bun --version)"
    return
  fi
  echo "ℹ Bun not found. Installing Bun..."
  if [ "$(uname -s)" = "Darwin" ] && command -v brew >/dev/null 2>&1; then
    brew install bun
  else
    # Non-interactive install via official script
    curl -fsSL https://bun.sh/install | bash
    # shellcheck disable=SC1090
    if [ -f "$HOME/.bun/bun" ] || [ -d "$HOME/.bun" ]; then
      export BUN_INSTALL="$HOME/.bun"
      export PATH="$BUN_INSTALL/bin:$PATH"
    fi
  fi
  if ! command -v bun >/dev/null 2>&1; then
    echo "❌ Failed to install Bun. Please install it manually from https://bun.sh" >&2
    exit 1
  fi
  echo "✔ Bun installed: $(bun --version)"
}

ensure_repo() {
  # Detect if running inside project root already
  if [ -f "package.json" ] && grep -q '"name"\s*:\s*"ritmex-bot"' package.json 2>/dev/null; then
    PROJECT_DIR="$PWD"
    echo "✔ Project detected in current directory: $PROJECT_DIR"
    return
  fi

  local REPO_URL="https://github.com/discountry/ritmex-bot.git"
  local TAR_URL="https://github.com/discountry/ritmex-bot/archive/refs/heads/main.tar.gz"
  local TARGET_DIR="${RITMEX_DIR:-ritmex-bot}"

  if [ -d "$TARGET_DIR" ] && [ -f "$TARGET_DIR/package.json" ]; then
    PROJECT_DIR="$(cd "$TARGET_DIR" && pwd)"
    echo "✔ Project directory found: $PROJECT_DIR"
    return
  fi

  echo "Fetching ritmex-bot sources..."
  if command -v git >/dev/null 2>&1; then
    git clone --depth=1 "$REPO_URL" "$TARGET_DIR"
  else
    echo "ℹ git not found; downloading tarball..."
    curl -fsSL "$TAR_URL" -o /tmp/ritmex-bot.tar.gz
    mkdir -p "$TARGET_DIR"
    tar -xzf /tmp/ritmex-bot.tar.gz --strip-components=1 -C "$TARGET_DIR"
    rm -f /tmp/ritmex-bot.tar.gz
  fi
  PROJECT_DIR="$(cd "$TARGET_DIR" && pwd)"
}

referral_notice() {
  echo
  echo "开始之前：请打开以下链接连接钱包加入战队，享受 30% 手续费优惠："
  echo "https://www.asterdex.com/zh-CN/referral/4665f3"
  echo
  # Wait for user to press Enter on a real TTY; don't fail if no TTY
  read -r -p "打开链接后按下回车继续..." _ < /dev/tty || true
}

install_deps() {
  echo "Installing dependencies with Bun..."
  (cd "$PROJECT_DIR" && bun install)
}

prompt_env() {
  echo
  echo "请输入 AsterDex API 凭证。"
  echo "如果你还没有 API，请打开以下链接创建后再继续："
  echo "https://www.asterdex.com/zh-CN/api-management"
  while true; do
    read -r -p "请输入 ASTER_API_KEY： " ASTER_API_KEY < /dev/tty
    [ -n "${ASTER_API_KEY:-}" ] && break
    echo "ASTER_API_KEY 不能为空，请重新输入。"
  done
  while true; do
    read -r -p "请输入 ASTER_API_SECRET： " ASTER_API_SECRET < /dev/tty
    [ -n "${ASTER_API_SECRET:-}" ] && break
    echo "ASTER_API_SECRET 不能为空，请重新输入。"
  done
  # 其余参数使用默认值（可在 .env 中自行修改）
  TRADE_SYMBOL="BTCUSDT"
  TRADE_AMOUNT="0.001"
  LOSS_LIMIT="0.03"
  KLINE_INTERVAL="1m"
}

write_env() {
  local env_file="$PROJECT_DIR/.env"
  echo "Writing ${env_file}..."
  cat > "${env_file}" <<EOF
# Generated by setup.sh
ASTER_API_KEY=${ASTER_API_KEY}
ASTER_API_SECRET=${ASTER_API_SECRET}
TRADE_SYMBOL=${TRADE_SYMBOL}
TRADE_AMOUNT=${TRADE_AMOUNT}
LOSS_LIMIT=${LOSS_LIMIT}
KLINE_INTERVAL=${KLINE_INTERVAL}

# Optional maker params (uncomment and tune as needed)
# MAKER_LOSS_LIMIT=0.03
# MAKER_BID_OFFSET=0
# MAKER_ASK_OFFSET=0
# MAKER_REFRESH_INTERVAL_MS=500
# MAKER_MAX_CLOSE_SLIPPAGE_PCT=0.05
# MAKER_PRICE_TICK=0.1
EOF
  echo "✔ .env created at ${env_file}"
}

main "$@"


