const { sequelize } = require('../models');
const { LivestockType, LivestockPolicy, LivestockClaim, User } = require('../models');

async function insertTestData() {
  try {
    console.log('开始插入生资保险测试数据...');

    // 创建测试用户（如果不存在）
    const [testUser] = await User.findOrCreate({
      where: { username: 'test_admin' },
      defaults: {
        username: 'test_admin',
        password: '$2b$10$example', // 示例密码哈希
        real_name: '测试管理员',
        email: 'test@example.com',
        phone: '13800138000',
        role_id: 1, // admin角色ID
        status: 'active'
      }
    });

    // 插入牲畜类型测试数据
    const livestockTypes = [
      {
        name: '奶牛',
        code: 'DAIRY_COW',
        description: '用于产奶的牛类',
        unit_price_min: 10000.00,
        unit_price_max: 20000.00,
        premium_rate: 0.05,
        status: 'active'
      },
      {
        name: '肉牛',
        code: 'BEEF_COW',
        description: '用于肉类生产的牛类',
        unit_price_min: 8000.00,
        unit_price_max: 16000.00,
        premium_rate: 0.04,
        status: 'active'
      },
      {
        name: '生猪',
        code: 'PIG',
        description: '用于肉类生产的猪类',
        unit_price_min: 1500.00,
        unit_price_max: 2500.00,
        premium_rate: 0.06,
        status: 'active'
      },
      {
        name: '羊',
        code: 'SHEEP',
        description: '包括山羊和绵羊',
        unit_price_min: 600.00,
        unit_price_max: 1000.00,
        premium_rate: 0.05,
        status: 'active'
      }
    ];

    console.log('插入牲畜类型数据...');
    const createdTypes = await LivestockType.bulkCreate(livestockTypes, {
      returning: true
    });
    console.log(`成功插入 ${createdTypes.length} 个牲畜类型`);

    // 插入生资保单测试数据
    const policies = [
      {
        policy_no: 'LP2024001',
        farmer_name: '张三',
        farmer_phone: '13800138001',
        farmer_id_card: '110101199001011234',
        livestock_type_id: createdTypes[0].id, // 奶牛
        livestock_count: 10,
        unit_value: 15000.00,
        total_value: 150000.00,
        premium_rate: 0.05,
        premium_amount: 7500.00,
        start_date: '2024-01-01',
        end_date: '2024-12-31',
        farm_name: '张三奶牛养殖场',
        farm_address: '北京市朝阳区某某村123号',
        farm_license: 'BJ2024001',
        policy_status: 'active',
        payment_status: 'paid',
        payment_date: '2024-01-01',
        notes: '优质奶牛保险',
        created_by: testUser.id,
        updated_by: testUser.id
      },
      {
        policy_no: 'LP2024002',
        farmer_name: '李四',
        farmer_phone: '13800138002',
        farmer_id_card: '110101199002021234',
        livestock_type_id: createdTypes[1].id, // 肉牛
        livestock_count: 20,
        unit_value: 12000.00,
        total_value: 240000.00,
        premium_rate: 0.04,
        premium_amount: 9600.00,
        start_date: '2024-02-01',
        end_date: '2025-01-31',
        farm_name: '李四肉牛养殖场',
        farm_address: '北京市海淀区某某村456号',
        farm_license: 'BJ2024002',
        policy_status: 'active',
        payment_status: 'paid',
        payment_date: '2024-02-01',
        notes: '肉牛养殖保险',
        created_by: testUser.id,
        updated_by: testUser.id
      },
      {
        policy_no: 'LP2024003',
        farmer_name: '王五',
        farmer_phone: '13800138003',
        farmer_id_card: '110101199003031234',
        livestock_type_id: createdTypes[2].id, // 生猪
        livestock_count: 100,
        unit_value: 2000.00,
        total_value: 200000.00,
        premium_rate: 0.06,
        premium_amount: 12000.00,
        start_date: '2024-03-01',
        end_date: '2025-02-28',
        farm_name: '王五生猪养殖场',
        farm_address: '北京市昌平区某某村789号',
        farm_license: 'BJ2024003',
        policy_status: 'active',
        payment_status: 'unpaid',
        notes: '生猪养殖保险',
        created_by: testUser.id,
        updated_by: testUser.id
      },
      {
        policy_no: 'LP2024004',
        farmer_name: '赵六',
        farmer_phone: '13800138004',
        farmer_id_card: '110101199004041234',
        livestock_type_id: createdTypes[3].id, // 羊
        livestock_count: 50,
        unit_value: 800.00,
        total_value: 40000.00,
        premium_rate: 0.05,
        premium_amount: 2000.00,
        start_date: '2024-04-01',
        end_date: '2025-03-31',
        farm_name: '赵六羊群养殖场',
        farm_address: '北京市房山区某某村101号',
        farm_license: 'BJ2024004',
        policy_status: 'active',
        payment_status: 'partial',
        payment_date: '2024-04-01',
        notes: '羊群保险',
        created_by: testUser.id,
        updated_by: testUser.id
      }
    ];

    console.log('插入生资保单数据...');
    const createdPolicies = await LivestockPolicy.bulkCreate(policies, {
      returning: true
    });
    console.log(`成功插入 ${createdPolicies.length} 个生资保单`);

    // 插入生资理赔测试数据
    const claims = [
      {
        claim_no: 'LC2024001',
        policy_id: createdPolicies[0].id,
        claim_amount: 30000.00,
        incident_date: '2024-06-15',
        incident_description: '奶牛因疾病死亡2头',
        claim_status: 'paid',
        investigator_id: testUser.id,
        investigation_report: '经现场调查，确认奶牛因疫病死亡，符合保险条款',
        investigation_date: '2024-06-20',
        reviewer_id: testUser.id,
        review_notes: '经核实，符合理赔条件',
        review_date: '2024-06-25',
        payment_date: '2024-07-01',
        payment_amount: 30000.00,
        supporting_documents: JSON.stringify([
          { type: 'death_certificate', url: '/documents/death_cert_001.pdf' },
          { type: 'veterinary_report', url: '/documents/vet_report_001.pdf' }
        ]),
        created_by: testUser.id,
        updated_by: testUser.id
      },
      {
        claim_no: 'LC2024002',
        policy_id: createdPolicies[1].id,
        claim_amount: 24000.00,
        incident_date: '2024-07-20',
        incident_description: '肉牛因自然灾害死亡2头',
        claim_status: 'investigating',
        investigator_id: testUser.id,
        investigation_report: '正在进行现场调查',
        investigation_date: '2024-07-25',
        supporting_documents: JSON.stringify([
          { type: 'incident_report', url: '/documents/incident_002.pdf' }
        ]),
        created_by: testUser.id,
        updated_by: testUser.id
      }
    ];

    console.log('插入生资理赔数据...');
    const createdClaims = await LivestockClaim.bulkCreate(claims, {
      returning: true
    });
    console.log(`成功插入 ${createdClaims.length} 个生资理赔记录`);

    console.log('生资保险测试数据插入完成！');
    console.log('数据统计:');
    console.log(`- 牲畜类型: ${createdTypes.length} 个`);
    console.log(`- 生资保单: ${createdPolicies.length} 个`);
    console.log(`- 理赔记录: ${createdClaims.length} 个`);

  } catch (error) {
    console.error('插入测试数据失败:', error);
  } finally {
    await sequelize.close();
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  insertTestData();
}

module.exports = insertTestData;