const { sequelize, Employee } = require('../models');

/**
 * 添加员工测试数据
 */
async function seedEmployees() {
  try {
    console.log('开始添加员工测试数据...');

    // 检查数据库连接
    await sequelize.authenticate();
    console.log('✅ 银行系统数据库连接成功');

    // 清空现有员工数据
    await Employee.destroy({
      where: {},
      force: true
    });
    console.log('✅ 清空现有员工数据');

    // 员工测试数据
    const employeesData = [
      {
        employeeNumber: 'EMP001',
        name: '刘超',
        phone: '15012341368',
        email: 'liuchao@bank.com',
        password: '123456',
        isLoanSpecialist: false,
        department: '风险管理部',
        position: '风险专员',
        status: 'active'
      },
      {
        employeeNumber: 'EMP002',
        name: '张明',
        phone: '13812345678',
        email: 'zhangming@bank.com',
        password: '123456',
        isLoanSpecialist: true,
        department: '信贷部',
        position: '贷款专员',
        status: 'active'
      },
      {
        employeeNumber: 'EMP003',
        name: '李红',
        phone: '13987654321',
        email: 'lihong@bank.com',
        password: '123456',
        isLoanSpecialist: true,
        department: '信贷部',
        position: '高级贷款专员',
        status: 'active'
      },
      {
        employeeNumber: 'EMP004',
        name: '王强',
        phone: '13611111111',
        email: 'wangqiang@bank.com',
        password: '123456',
        isLoanSpecialist: false,
        department: '运营部',
        position: '运营专员',
        status: 'active'
      },
      {
        employeeNumber: 'EMP005',
        name: '陈静',
        phone: '13722222222',
        email: 'chenjing@bank.com',
        password: '123456',
        isLoanSpecialist: true,
        department: '信贷部',
        position: '贷款专员',
        status: 'inactive'
      },
      {
        employeeNumber: 'EMP006',
        name: '赵磊',
        phone: '13833333333',
        email: 'zhaolei@bank.com',
        password: '123456',
        isLoanSpecialist: false,
        department: '技术部',
        position: '系统管理员',
        status: 'active'
      },
      {
        employeeNumber: 'EMP007',
        name: '孙丽',
        phone: '13944444444',
        email: 'sunli@bank.com',
        password: '123456',
        isLoanSpecialist: true,
        department: '信贷部',
        position: '贷款专员',
        status: 'locked'
      },
      {
        employeeNumber: 'EMP008',
        name: '周涛',
        phone: '13555555555',
        email: 'zhoutao@bank.com',
        password: '123456',
        isLoanSpecialist: false,
        department: '财务部',
        position: '财务专员',
        status: 'active'
      },
      {
        employeeNumber: 'EMP009',
        name: '吴敏',
        phone: '13466666666',
        email: 'wumin@bank.com',
        password: '123456',
        isLoanSpecialist: true,
        department: '信贷部',
        position: '贷款专员',
        status: 'active'
      },
      {
        employeeNumber: 'EMP010',
        name: '郑华',
        phone: '13377777777',
        email: 'zhenghua@bank.com',
        password: '123456',
        isLoanSpecialist: false,
        department: '人事部',
        position: '人事专员',
        status: 'active'
      }
    ];

    // 创建员工
    const employees = await Employee.bulkCreate(employeesData);
    console.log(`✅ 成功创建${employees.length}个员工`);

    // 统计信息
    const activeCount = await Employee.count({ where: { status: 'active' } });
    const inactiveCount = await Employee.count({ where: { status: 'inactive' } });
    const lockedCount = await Employee.count({ where: { status: 'locked' } });
    const loanSpecialistCount = await Employee.count({ where: { isLoanSpecialist: true } });

    console.log('\n📊 员工数据统计：');
    console.log(`- 活跃员工：${activeCount}个`);
    console.log(`- 停用员工：${inactiveCount}个`);
    console.log(`- 锁定员工：${lockedCount}个`);
    console.log(`- 贷款专员：${loanSpecialistCount}个`);
    console.log(`- 总员工数：${employees.length}个`);

    console.log('\n🎉 员工测试数据添加完成！');

  } catch (error) {
    console.error('❌ 添加员工测试数据失败:', error);
    throw error;
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  seedEmployees()
    .then(() => {
      console.log('脚本执行完成');
      process.exit(0);
    })
    .catch((error) => {
      console.error('❌ 脚本执行失败:', error);
      process.exit(1);
    });
}

module.exports = seedEmployees;
