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

async function createInsuranceTypes() {
  try {
    await sequelize.authenticate();
    console.log('数据库连接成功');

    // 检查是否已有保险类型
    const existingTypes = await InsuranceType.findAll();
    console.log('现有保险类型数量:', existingTypes.length);

    if (existingTypes.length === 0) {
      // 创建测试保险类型
      const insuranceTypes = [
        {
          name: '养殖保险',
          description: '为养殖业提供风险保障',
          coverage_amount_min: 10000.00,
          coverage_amount_max: 500000.00,
          premium_rate: 0.05,
          status: 'active'
        },
        {
          name: '意外保险',
          description: '提供意外事故保障',
          coverage_amount_min: 50000.00,
          coverage_amount_max: 300000.00,
          premium_rate: 0.02,
          status: 'active'
        },
        {
          name: '健康保险',
          description: '提供健康医疗保障',
          coverage_amount_min: 20000.00,
          coverage_amount_max: 200000.00,
          premium_rate: 0.03,
          status: 'active'
        }
      ];

      const createdTypes = await InsuranceType.bulkCreate(insuranceTypes);
      console.log('✓ 创建保险类型成功，数量:', createdTypes.length);
      
      createdTypes.forEach(type => {
        console.log(`  - ${type.name} (ID: ${type.id})`);
      });
    } else {
      console.log('✓ 保险类型已存在');
      existingTypes.forEach(type => {
        console.log(`  - ${type.name} (ID: ${type.id})`);
      });
    }

  } catch (error) {
    console.error('创建保险类型失败:', error);
  } finally {
    await sequelize.close();
    console.log('数据库连接关闭');
  }
}

createInsuranceTypes();