const { Sequelize } = require('sequelize');
require('dotenv').config();

// 从环境变量获取数据库配置 - 强制使用正确的数据库
const DB_DIALECT = 'mysql';
const DB_HOST = '129.211.213.226';
const DB_PORT = 9527;
const DB_NAME = 'nxxmdata';
const DB_USER = 'root';
const DB_PASSWORD = 'aiotAiot123!';

// 创建Sequelize实例
const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASSWORD, {
  host: DB_HOST,
  port: DB_PORT,
  dialect: DB_DIALECT,
  logging: false,
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },
  define: {
    timestamps: true,
    charset: 'utf8mb4'
  },
  timezone: '+08:00'
});

// 测试数据库连接
const testConnection = async () => {
  try {
    await sequelize.authenticate();
    console.log('✅ 数据库连接成功');
    return true;
  } catch (err) {
    console.error('❌ 数据库连接失败:', err.message);
    return false;
  }
};

module.exports = {
  sequelize,
  testConnection
};
