/**
 * Email Generator - 邮箱生成器
 */

const { randomInt } = require('../../utils');

class EmailGenerator {
  constructor() {
    this.domains = [
      'qichen111.asia'
    ];
  }

  /**
   * 生成邮箱地址
   * @param {Object} options - 选项
   * @returns {string}
   */
  generate(options = {}) {
    const domain = options.domain || this.getRandomDomain();
    const prefix = this.generatePrefix(options.pattern);
    
    return `${prefix}@${domain}`;
  }

  /**
   * 生成邮箱前缀
   * @param {string} pattern - 模式
   * @returns {string}
   */
  generatePrefix(pattern) {
    if (pattern) {
      // 支持模式替换，如 "user_{random}"
      return pattern.replace('{random}', this.generateRandomString());
    }

    // 默认格式: 纯随机字符串（不带user前缀）
    const length = randomInt(8, 12);
    return this.generateRandomString(length);
  }

  /**
   * 生成随机字符串
   * @param {number} length - 长度
   * @returns {string}
   */
  generateRandomString(length = 8) {
    return Math.random().toString(36).substring(2, 2 + length);
  }

  /**
   * 获取随机域名
   * @returns {string}
   */
  getRandomDomain() {
    return this.domains[randomInt(0, this.domains.length - 1)];
  }

  /**
   * 批量生成
   * @param {number} count - 数量
   * @param {Object} options - 选项
   * @returns {Array}
   */
  generateBatch(count, options = {}) {
    const emails = [];
    for (let i = 0; i < count; i++) {
      emails.push(this.generate(options));
    }
    return emails;
  }
}

module.exports = EmailGenerator;
