#!/usr/bin/env bash
# 本機 API OAuth callback 需 HTTPS（Meta 會拒絕 http:// redirect，錯誤碼 1349187）
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ENV_FILE="$ROOT/deploy/.env.dev"
: "${HAIXUN_API_PORT:=8890}"

if ! command -v ngrok >/dev/null 2>&1; then
  echo "請先安裝 ngrok：https://ngrok.com/download" >&2
  exit 1
fi

echo "啟動 ngrok → 127.0.0.1:${HAIXUN_API_PORT}"
echo "完成後請把下列 URI 登記到 Meta App Dashboard → Redirect Callback URLs："
echo "  https://<你的-ngrok網域>/api/v1/threads-accounts/oauth/callback"
echo ""
echo "並更新 deploy/.env.dev："
echo "  THREADS_REDIRECT_URI=https://<你的-ngrok網域>/api/v1/threads-accounts/oauth/callback"
echo ""
echo "授權導回時若看到 ngrok「Visit Site」，請點進去讓 callback 打到 API。"
echo ""

exec ngrok http "${HAIXUN_API_PORT}" --log=stdout