// Test config — read from env so the same scripts run locally and in CI.
//
// Required:
//   BASE_URL          gateway base URL (default http://localhost:8888)
//   MAILHOG_URL       MailHog HTTP API (default http://localhost:8025)
//   REDIS_ADDR        Redis address for SMS OTP read (default localhost:6379)
//
// Optional (test data tuning):
//   TENANT_SLUG          tenant used for register payloads (default k6-tenant)
//   INVITE_CODE          invite code for tenants requiring it (default K6INVITE)
//   ADMIN_EMAIL          seeded admin login for rbac journeys
//   ADMIN_PASSWORD
//   ADMIN_ACCESS_TOKEN   pre-issued admin access_token (from k6-seed-admin);
//                        used by rbac_admin.js to bypass /auth/login when
//                        ZITADEL v2 password grant is unavailable.
//   ADMIN_REFRESH_TOKEN  matching refresh token (optional, future use).
//
// All scripts must import from here; do NOT hard-code URLs/credentials.
export const cfg = {
  baseUrl: __ENV.BASE_URL || 'http://localhost:8888',
  mailhogUrl: __ENV.MAILHOG_URL || 'http://localhost:8025',
  redisAddr: __ENV.REDIS_ADDR || 'localhost:6379',
  tenantSlug: __ENV.TENANT_SLUG || 'k6-tenant',
  inviteCode: __ENV.INVITE_CODE || 'K6INVITE',
  adminEmail: __ENV.ADMIN_EMAIL || '',
  adminPassword: __ENV.ADMIN_PASSWORD || '',
  adminAccessToken: __ENV.ADMIN_ACCESS_TOKEN || '',
  adminRefreshToken: __ENV.ADMIN_REFRESH_TOKEN || '',
  // OTP polling parameters (MailHog / Redis)
  otpPollIntervalMs: parseInt(__ENV.OTP_POLL_INTERVAL_MS || '300', 10),
  otpPollTimeoutMs: parseInt(__ENV.OTP_POLL_TIMEOUT_MS || '5000', 10),
  /** 與 etc/gateway.k6.yaml Member.OTP.ResendCooldownSeconds 一致 */
  resendCooldownSeconds: parseInt(__ENV.RESEND_COOLDOWN_SECONDS || '60', 10),
};

// Build a unique-ish identity per VU+iteration so concurrent runs do not collide.
export function unique(prefix) {
  const ts = Date.now();
  const vu = (typeof __VU !== 'undefined' && __VU) || 0;
  const iter = (typeof __ITER !== 'undefined' && __ITER) || 0;
  return `${prefix}-${ts}-${vu}-${iter}`;
}

// Common biz code: 102000 = success envelope (see internal/library/errors/code).
export const SUCCESS_CODE = 102000;
