/**
 * Name Generator - 姓名生成器
 */

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

class NameGenerator {
  constructor() {
    // 常见英文名字
    this.firstNames = {
      male: [
        'James', 'John', 'Robert', 'Michael', 'William',
        'David', 'Richard', 'Joseph', 'Thomas', 'Charles',
        'Daniel', 'Matthew', 'Anthony', 'Mark', 'Donald',
        'Steven', 'Paul', 'Andrew', 'Joshua', 'Kenneth'
      ],
      female: [
        'Mary', 'Patricia', 'Jennifer', 'Linda', 'Elizabeth',
        'Barbara', 'Susan', 'Jessica', 'Sarah', 'Karen',
        'Nancy', 'Lisa', 'Betty', 'Margaret', 'Sandra',
        'Ashley', 'Kimberly', 'Emily', 'Donna', 'Michelle'
      ],
      neutral: [
        'Alex', 'Jordan', 'Taylor', 'Casey', 'Riley',
        'Morgan', 'Parker', 'Avery', 'Quinn', 'Skyler'
      ]
    };

    // 常见英文姓氏
    this.lastNames = [
      'Smith', 'Johnson', 'Williams', 'Brown', 'Jones',
      'Garcia', 'Miller', 'Davis', 'Rodriguez', 'Martinez',
      'Hernandez', 'Lopez', 'Gonzalez', 'Wilson', 'Anderson',
      'Thomas', 'Taylor', 'Moore', 'Jackson', 'Martin',
      'Lee', 'Perez', 'Thompson', 'White', 'Harris',
      'Sanchez', 'Clark', 'Ramirez', 'Lewis', 'Robinson'
    ];

    // 中文姓名
    this.chineseFirstNames = ['伟', '芳', '娜', '秀英', '敏', '静', '丽', '强', '磊', '军'];
    this.chineseLastNames = ['王', '李', '张', '刘', '陈', '杨', '赵', '黄', '周', '吴'];
  }

  /**
   * 生成名字
   * @param {Object} options - 选项
   * @returns {string}
   */
  generateFirstName(options = {}) {
    const { gender, locale } = options;

    if (locale === 'zh-CN') {
      return this.generateChineseFirstName();
    }

    // 英文名字
    let pool;
    if (gender === 'male') {
      pool = this.firstNames.male;
    } else if (gender === 'female') {
      pool = this.firstNames.female;
    } else {
      // 随机选择性别或中性
      const allNames = [
        ...this.firstNames.male,
        ...this.firstNames.female,
        ...this.firstNames.neutral
      ];
      pool = allNames;
    }

    return pool[randomInt(0, pool.length - 1)];
  }

  /**
   * 生成姓氏
   * @param {Object} options - 选项
   * @returns {string}
   */
  generateLastName(options = {}) {
    const { locale } = options;

    if (locale === 'zh-CN') {
      return this.generateChineseLastName();
    }

    return this.lastNames[randomInt(0, this.lastNames.length - 1)];
  }

  /**
   * 生成完整姓名
   * @param {Object} options - 选项
   * @returns {Object}
   */
  generateFullName(options = {}) {
    const firstName = this.generateFirstName(options);
    const lastName = this.generateLastName(options);

    return {
      firstName,
      lastName,
      fullName: options.locale === 'zh-CN' 
        ? `${lastName}${firstName}` 
        : `${firstName} ${lastName}`
    };
  }

  /**
   * 生成中文名字
   * @returns {string}
   */
  generateChineseFirstName() {
    return this.chineseFirstNames[randomInt(0, this.chineseFirstNames.length - 1)];
  }

  /**
   * 生成中文姓氏
   * @returns {string}
   */
  generateChineseLastName() {
    return this.chineseLastNames[randomInt(0, this.chineseLastNames.length - 1)];
  }

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

module.exports = NameGenerator;
