/**
 * Account Data Generator - 账号数据生成器
 * 统一的数据生成接口
 */

const EmailGenerator = require('./email-generator');
const UsernameGenerator = require('./username-generator');
const PasswordGenerator = require('./password-generator');
const PhoneGenerator = require('./phone-generator');
const NameGenerator = require('./name-generator');
const CardGenerator = require('../card-generator');

class AccountDataGenerator {
  constructor() {
    this.emailGen = new EmailGenerator();
    this.usernameGen = new UsernameGenerator();
    this.passwordGen = new PasswordGenerator();
    this.phoneGen = new PhoneGenerator();
    this.nameGen = new NameGenerator();
    this.cardGen = new CardGenerator();
  }

  /**
   * 生成完整的账号数据
   * @param {Object} options - 配置选项
   * @returns {Object}
   */
  generateAccount(options = {}) {
    const name = this.generateName(options.name);
    const email = this.generateEmail(options.email);
    
    // 根据策略生成密码
    let password;
    const passwordStrategy = options.password?.strategy || 'email';
    if (passwordStrategy === 'email') {
      // 使用邮箱地址作为密码
      password = email;
    } else {
      // 使用随机密码
      password = this.generatePassword(options.password);
    }
    
    const account = {
      firstName: name.firstName,
      lastName: name.lastName,
      fullName: name.fullName,
      email: email,
      username: this.generateUsername(options.username),
      password: password,
      passwordStrategy: passwordStrategy,
      timestamp: new Date().toISOString()
    };

    // 可选字段
    if (options.includePhone !== false) {
      account.phone = this.generatePhone(options.phone);
    }

    if (options.includeCard) {
      account.card = this.generateCard(options.card);
    }

    return account;
  }

  /**
   * 生成邮箱
   * @param {Object} options - 选项
   * @returns {string}
   */
  generateEmail(options = {}) {
    return this.emailGen.generate(options);
  }

  /**
   * 生成用户名
   * @param {Object} options - 选项
   * @returns {string}
   */
  generateUsername(options = {}) {
    return this.usernameGen.generate(options);
  }

  /**
   * 生成密码
   * @param {Object} options - 选项
   * @returns {string}
   */
  generatePassword(options = {}) {
    return this.passwordGen.generate(options);
  }

  /**
   * 生成手机号
   * @param {Object} options - 选项
   * @returns {string}
   */
  generatePhone(options = {}) {
    return this.phoneGen.generate(options);
  }

  /**
   * 生成姓名
   * @param {Object} options - 选项
   * @returns {Object}
   */
  generateName(options = {}) {
    return this.nameGen.generateFullName(options);
  }

  /**
   * 生成信用卡
   * @param {Object} options - 选项
   * @returns {Object}
   */
  generateCard(options = {}) {
    const type = options.type || 'visa';
    return this.cardGen.generate(type);
  }

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

module.exports = AccountDataGenerator;
