#!/usr/bin/env node

/**
 * 测试数据初始化脚本
 * 用于开发环境创建测试数据
 */

const mysql = require('mysql2/promise');
const bcrypt = require('bcryptjs');
const config = require('../config/env');

// 引入database.js配置
const dbConfig = require('../src/config/database').pool.config;

// 数据库配置已从database.js导入

// 测试数据
const testData = {
  admins: [
    {
      username: 'admin',
      password: 'admin123',
      email: 'admin@jiebanke.com',
      nickname: '超级管理员',
      role: 'super_admin',
      status: 1
    },
    {
      username: 'manager',
      password: 'manager123',
      email: 'manager@jiebanke.com',
      nickname: '运营经理',
      role: 'admin',
      status: 1
    }
  ],
  users: [
    {
      username: 'user1',
      password: 'user123',
      email: 'user1@jiebanke.com',
      nickname: '旅行爱好者',
      avatar: null,
      user_type: '普通用户',
      status: 1
    },
    {
      username: 'merchant1',
      password: 'merchant123',
      email: 'merchant1@jiebanke.com',
      nickname: '农家乐老板',
      avatar: null,
      user_type: '商家',
      status: 1
    }
  ]
};

async function initTestData() {
  let connection;
  
  try {
    console.log('🚀 开始初始化测试数据...');
    
    // 创建数据库连接
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');
    
    // 插入管理员数据
    console.log('📝 插入管理员数据...');
    for (const admin of testData.admins) {
      const hashedPassword = await bcrypt.hash(admin.password, 10);
      const [result] = await connection.execute(
        `INSERT INTO admins (username, password, email, nickname, role, status, created_at, updated_at) 
         VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())`,
        [admin.username, hashedPassword, admin.email, admin.nickname, admin.role, admin.status]
      );
      console.log(`  创建管理员: ${admin.username} (ID: ${result.insertId})`);
    }
    
    // 插入用户数据
    console.log('👥 插入用户数据...');
    for (const user of testData.users) {
      const hashedPassword = await bcrypt.hash(user.password, 10);
      const [result] = await connection.execute(
        `INSERT INTO users (username, password, email, nickname, avatar, user_type, status, created_at, updated_at) 
         VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`,
        [user.username, hashedPassword, user.email, user.nickname, user.avatar, user.user_type, user.status]
      );
      console.log(`  创建用户: ${user.username} (ID: ${result.insertId})`);
    }
    
    console.log('\n🎉 测试数据初始化完成！');
    console.log('📋 可用测试账号:');
    console.log('   管理员账号: admin / admin123');
    console.log('   运营账号: manager / manager123');
    console.log('   普通用户: user1 / user123');
    console.log('   商家用户: merchant1 / merchant123');
    
  } catch (error) {
    console.error('❌ 初始化测试数据失败:', error.message);
    if (error.code === 'ER_NO_SUCH_TABLE') {
      console.log('💡 提示: 请先运行数据库迁移脚本创建表结构');
    }
  } finally {
    if (connection) {
      await connection.end();
    }
  }
}

// 如果是直接运行此文件，则执行初始化
if (require.main === module) {
  initTestData()
    .then(() => process.exit(0))
    .catch(() => process.exit(1));
}

module.exports = { initTestData };