const { sequelize } = require('../config/database');

async function insertTestData() {
  try {
    // 确保使用正确的数据库
    await sequelize.query('USE insurance_data');
    console.log('✅ 已切换到 insurance_data 数据库');
    
    console.log('开始插入测试数据...');
    
    // 1. 插入牲畜类型数据
    await sequelize.query(`
      INSERT IGNORE INTO livestock_types (name, code, description, unit_price_min, unit_price_max, premium_rate, status)
      VALUES
      ('牛', 'CATTLE', '肉牛、奶牛等牛类牲畜', 5000.00, 30000.00, 0.0050, 'active'),
      ('羊', 'SHEEP', '绵羊、山羊等羊类牲畜', 500.00, 3000.00, 0.0060, 'active'),
      ('猪', 'PIG', '生猪、种猪等猪类牲畜', 1000.00, 5000.00, 0.0040, 'active'),
      ('鸡', 'CHICKEN', '肉鸡、蛋鸡等鸡类家禽', 20.00, 100.00, 0.0080, 'active'),
      ('鸭', 'DUCK', '肉鸭、蛋鸭等鸭类家禽', 30.00, 150.00, 0.0070, 'active')
    `);
    console.log('✅ 牲畜类型数据插入成功');

    // 2. 插入生资保单数据
    await sequelize.query(`
      INSERT INTO livestock_policies (
        policy_no, livestock_type_id, policyholder_name, policyholder_id_card, policyholder_phone, 
        policyholder_address, farm_address, livestock_count, unit_value, total_value, 
        premium_amount, start_date, end_date, policy_status, 
        created_by, updated_by
      ) VALUES
      ('202209312456789', 1, '张三', '150102198001011234', '13800138001', '内蒙古自治区呼和浩特市赛罕区昭乌达路', '内蒙古自治区呼和浩特市赛罕区昭乌达路养殖场', 50, 8000.00, 400000.00, 2000.00, '2023-03-05', '2024-03-04', 'active', 1, 1),
      ('202209314567890', 2, '李四', '150203198002022345', '13800138002', '内蒙古自治区包头市昆都仑区钢铁大街', '内蒙古自治区包头市昆都仑区钢铁大街养殖场', 100, 1200.00, 120000.00, 720.00, '2023-03-10', '2024-03-09', 'active', 1, 1),
      ('202209315432100', 3, '王五', '150602198003033456', '13800138003', '内蒙古自治区鄂尔多斯市东胜区铁西路', '内蒙古自治区鄂尔多斯市东胜区铁西路养殖场', 200, 2500.00, 500000.00, 2000.00, '2023-02-15', '2024-02-14', 'active', 1, 1),
      ('202203316542100', 1, '赵六', '150502198004044567', '13800138004', '内蒙古自治区通辽市科尔沁区建国路', '内蒙古自治区通辽市科尔沁区建国路养殖场', 30, 12000.00, 360000.00, 1800.00, '2023-02-20', '2024-02-19', 'active', 1, 1),
      ('202203311087654', 2, '孙七', '150802198005055678', '13800138005', '内蒙古自治区巴彦淖尔市临河区胜利路', '内蒙古自治区巴彦淖尔市临河区胜利路养殖场', 80, 1500.00, 120000.00, 720.00, '2023-01-30', '2024-01-29', 'active', 1, 1),
      ('202203178001234', 4, '周八', '150902198006066789', '13800138006', '内蒙古自治区乌兰察布市集宁区新华街', '内蒙古自治区乌兰察布市集宁区新华街养殖场', 1000, 50.00, 50000.00, 400.00, '2023-01-15', '2024-01-14', 'active', 1, 1),
      ('202203112345678', 5, '吴九', '152502198007077890', '13800138007', '内蒙古自治区锡林郭勒盟锡林浩特市额尔敦路', '内蒙古自治区锡林郭勒盟锡林浩特市额尔敦路养殖场', 800, 80.00, 64000.00, 448.00, '2023-01-10', '2024-01-09', 'active', 1, 1),
      ('202203134567890', 1, '郑十', '150702198008088901', '13800138008', '内蒙古自治区呼伦贝尔市海拉尔区胜利大街', '内蒙古自治区呼伦贝尔市海拉尔区胜利大街养殖场', 25, 15000.00, 375000.00, 1875.00, '2022-12-20', '2023-12-19', 'expired', 1, 1),
      ('202203154321098', 3, '陈十一', '150402198009099012', '13800138009', '内蒙古自治区赤峰市红山区钢铁街', '内蒙古自治区赤峰市红山区钢铁街养殖场', 150, 3000.00, 450000.00, 1800.00, '2022-12-15', '2023-12-14', 'expired', 1, 1)
    `);
    console.log('✅ 生资保单数据插入成功');

    // 3. 插入理赔数据
    await sequelize.query(`
      INSERT INTO livestock_claims (
        claim_no, policy_id, claim_type, incident_date, report_date,
        incident_description, incident_location, affected_count,
        claim_amount, claim_status, investigator_id,
        reviewer_id, created_by, updated_by
      ) VALUES
      ('CL202303050001', 1, 'disease', '2023-03-01', '2023-03-02', '牛群感染口蹄疫，导致5头牛死亡', '内蒙古自治区呼和浩特市赛罕区昭乌达路养殖场', 5, 35000.00, 'approved', 1, 1, 1, 1),
      ('CL202303100002', 2, 'natural_disaster', '2023-03-08', '2023-03-09', '暴雪导致羊舍倒塌，造成10只羊死亡', '内蒙古自治区包头市昆都仑区钢铁大街养殖场', 10, 10000.00, 'investigating', 1, NULL, 1, 1),
      ('CL202302200003', 4, 'accident', '2023-02-18', '2023-02-19', '运输车辆事故，导致3头牛受伤', '内蒙古自治区通辽市科尔沁区建国路', 3, 12000.00, 'pending', NULL, NULL, 1, 1)
    `);
    console.log('✅ 理赔数据插入成功');

    // 验证数据插入结果
    const [policyCount] = await sequelize.query("SELECT COUNT(*) as count FROM livestock_policies");
    const [claimCount] = await sequelize.query("SELECT COUNT(*) as count FROM livestock_claims");
    const [typeCount] = await sequelize.query("SELECT COUNT(*) as count FROM livestock_types");
    
    console.log(`✅ 数据插入完成:`);
    console.log(`   - 牲畜类型: ${typeCount[0].count} 条`);
    console.log(`   - 生资保单: ${policyCount[0].count} 条`);
    console.log(`   - 理赔记录: ${claimCount[0].count} 条`);
    
  } catch (error) {
    console.error('❌ 插入测试数据失败:', error.message);
    if (error.errors) {
      console.error('详细错误信息:');
      error.errors.forEach(err => {
        console.error(`- ${err.path}: ${err.message}`);
      });
    }
    console.error('完整错误:', error);
  } finally {
    await sequelize.close();
  }
}

insertTestData();