#!/bin/bash
set -e

# 设置环境变量
export SERVER_PORT="5000"
export AUTH_JWT_SECRET="super-secret-jwt-token-with-at-least-32-characters-long"
export AUTH_JWT_ALGORITHM="HS256"
export DATABASE_URL="postgres://supabase_admin:postgres@127.0.0.1:5432/postgres"
#export DATABASE_POOL_URL="postgresql://postgres:postgres@127.0.0.1:6432/postgres"
#export DB_INSTALL_ROLES="true"
export STORAGE_BACKEND="file"
export FILE_SIZE_LIMIT=52428800     # 50 * 1024 * 1024
export FILE_STORAGE_BACKEND_PATH="/var/lib/storage" 
export DB_INSTALL_ROLES="true"

# ✅ 明确告诉系统是单租户，避免 undefined
export MULTI_TENANT="false"
export TENANT_ID="storage-single-tenant"
export REGION="chatdesk"
export SUPABASE_PROJECT_REF="storage-single-tenant"

# ✅ 日志调试（可选）
echo "[DEBUG] MULTI_TENANT=$MULTI_TENANT"
echo "[DEBUG] TENANT_ID=$TENANT_ID"
echo "[DEBUG] FILE_STORAGE_BACKEND_PATH=$FILE_STORAGE_BACKEND_PATH"


# 等待 gotrue 服务可用（健康检查）
echo "[storage-api] Waiting for GoTrue at /user..."
until curl -s -o /dev/null -w "%{http_code}" http://localhost:9999/user | grep -qE "^(200|401|403)$"; do
  echo "[storage-api] Still waiting for GoTrue..."
  sleep 2
done

# 启动 Node 应用
exec node /supabase/storage-api/dist/start/server.js
