import { ConfigService } from '@nestjs/config';

export interface ChuanglanConfig {
  account: string;
  password: string;
  endpoint: string;
  connectTimeoutMs: number;
  readTimeoutMs: number;
}

export interface SmsBusinessConfig {
  codeTtlSeconds: number;
  rateLimitSeconds: number;
  /** 为 true 时在日志输出验证码与完整 sessionId（仅联调，生产务必关闭） */
  debugLogCode: boolean;
}

export function loadChuanglanConfig(config: ConfigService): ChuanglanConfig | null {
  const account = config.get<string>('CHUANGLAN_ACCOUNT');
  const password = config.get<string>('CHUANGLAN_PASSWORD');
  if (!account || !password) {
    return null;
  }
  return {
    account,
    password,
    endpoint: config.get('CHUANGLAN_ENDPOINT', 'https://sgap.253.com/send/sms'),
    connectTimeoutMs: Number(config.get('CHUANGLAN_CONNECT_TIMEOUT_MS', 10_000)),
    readTimeoutMs: Number(config.get('CHUANGLAN_READ_TIMEOUT_MS', 10_000)),
  };
}

export function loadSmsBusinessConfig(config: ConfigService): SmsBusinessConfig {
  const debugRaw = config.get<string>('SMS_DEBUG_LOG_CODE', 'false');
  return {
    codeTtlSeconds: Number(config.get('SMS_CODE_TTL_SECONDS', 300)),
    rateLimitSeconds: Number(config.get('SMS_RATE_LIMIT_SECONDS', 60)),
    debugLogCode: debugRaw === 'true' || debugRaw === '1',
  };
}
