const { sequelize, LoanProduct } = require('../models');
const seedLoanProducts = require('./seed-loan-products');

async function setupLoanProducts() {
  try {
    console.log('开始设置贷款商品表...');

    // 测试数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');

    // 同步模型（创建表）
    await sequelize.sync({ force: false });
    console.log('贷款商品表同步成功');

    // 添加测试数据
    await seedLoanProducts();

    console.log('贷款商品设置完成');

  } catch (error) {
    console.error('设置贷款商品失败:', error);
    throw error;
  } finally {
    await sequelize.close();
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  setupLoanProducts()
    .then(() => {
      console.log('贷款商品设置完成');
      process.exit(0);
    })
    .catch((error) => {
      console.error('设置贷款商品失败:', error);
      process.exit(1);
    });
}

module.exports = setupLoanProducts;
