#!/bin/bash

set -e

echo "📥 克隆 Microsoft-Rewards-Script 项目..."
cd /root
git clone https://github.com/TheNetsky/Microsoft-Rewards-Script.git

cd Microsoft-Rewards-Script/src

echo "👤 请输入你要添加的账户数量："
read -p "账户数量（例如 1 或 2 或更多）: " account_count

echo "📝 开始输入账户信息..."

accounts="["

for ((i = 1; i <= account_count; i++)); do
  echo "第 $i 个账户："
  read -p "  邮箱: " email
  read -p "  密码: " password

  account_entry=$(cat <<EOF
{
    "email": "$email",
    "password": "$password",
    "proxy": {
        "proxyAxios": false,
        "url": "",
        "port": 0,
        "username": "",
        "password": ""
    }
}
EOF
)

  accounts+="$account_entry"
  if [[ $i -lt $account_count ]]; then
    accounts+=","
  fi
done

accounts+="]"

echo "$accounts" > accounts.json
echo "✅ accounts.json 已创建完成。"

echo "🛠️ 正在替换 config.json..."

cat > config.json <<EOF
{
    "baseURL": "https://rewards.bing.com",
    "sessionPath": "sessions",
    "headless": true,
    "parallel": false,
    "runOnZeroPoints": false,
    "clusters": 1,
    "saveFingerprint": {
        "mobile": false,
        "desktop": false
    },
    "workers": {
        "doDailySet": true,
        "doMorePromotions": true,
        "doPunchCards": true,
        "doDesktopSearch": true,
        "doMobileSearch": true,
        "doDailyCheckIn": true,
        "doReadToEarn": true
    },
    "searchOnBingLocalQueries": false,
    "globalTimeout": "30s",
    "searchSettings": {
        "useGeoLocaleQueries": false,
        "scrollRandomResults": true,
        "clickRandomResults": true,
        "searchDelay": {
            "min": "3min",
            "max": "5min"
        },
        "retryMobileSearchAmount": 2
    },
    "logExcludeFunc": [
        "SEARCH-CLOSE-TABS"
    ],
    "webhookLogExcludeFunc": [
        "SEARCH-CLOSE-TABS"
    ],
    "proxy": {
        "proxyGoogleTrends": true,
        "proxyBingTerms": true
    },
    "webhook": {
        "enabled": false,
        "url": ""
    }
}
EOF

echo "✅ config.json 已成功替换。"

echo "⚙️ 修改 compose.yaml volume 路径为当前目录..."

COMPOSE_FILE="../compose.yaml"
cp "$COMPOSE_FILE" "${COMPOSE_FILE}.bak"

WORKDIR=$(pwd)

sed -i "s|/path/to/accounts.json|$WORKDIR/accounts.json|g" "$COMPOSE_FILE"
sed -i "s|/path/to/config.json|$WORKDIR/config.json|g" "$COMPOSE_FILE"
sed -i "s|/path/to/sessions|$WORKDIR/sessions|g" "$COMPOSE_FILE"

echo "✅ compose.yaml 修改完成。"

echo "🚀 构建并启动容器中..."
cd ..
docker-compose up -d

echo "🎉 所有步骤已完成！Microsoft-Rewards-Script 正在运行中。"
