/**
 * 添加贷款解押测试数据
 * @file add-loan-release-test-data.js
 * @description 为贷款解押功能添加测试数据
 */
const { LoanRelease, LoanReleaseHistory, User } = require('./models');

async function addTestData() {
  try {
    console.log('🔄 开始添加贷款解押测试数据...\n');

    // 获取管理员用户
    const adminUser = await User.findOne({ where: { username: 'admin' } });
    if (!adminUser) {
      console.log('❌ 未找到管理员用户，请先创建用户');
      return;
    }

    // 清空现有数据
    await LoanReleaseHistory.destroy({ where: {} });
    await LoanRelease.destroy({ where: {} });
    console.log('🗑️ 清空现有测试数据');

    // 创建解押申请测试数据
    const releases = [
      {
        application_number: '20240227145555918',
        contract_id: null,
        customer_name: '刘超',
        customer_phone: '13800138000',
        customer_id_card: '511123199001013017',
        farmer_name: '刘超',
        product_name: '中国工商银行扎旗支行"畜禽活体抵押"',
        collateral_type: 'livestock',
        collateral_description: '优质肉牛',
        collateral_value: 150000.00,
        loan_amount: 100000.00,
        release_amount: 10000.00,
        release_quantity: '10头',
        application_reason: '需要资金周转，申请部分解押',
        status: 'released',
        application_date: new Date('2024-02-27 14:55:55'),
        process_date: new Date('2024-02-27 15:30:00'),
        complete_date: new Date('2024-02-27 16:00:00'),
        processor_id: adminUser.id,
        processor_name: adminUser.real_name || adminUser.username,
        process_comment: '审核通过，同意解押',
        remark: '客户信用良好'
      },
      {
        application_number: '20240226113416302',
        contract_id: null,
        customer_name: '李四',
        customer_phone: '13900139000',
        customer_id_card: '511123199002020002',
        farmer_name: '李四',
        product_name: '惠农贷',
        collateral_type: 'livestock',
        collateral_description: '优质奶牛',
        collateral_value: 200000.00,
        loan_amount: 150000.00,
        release_amount: 0.00,
        release_quantity: '10头',
        application_reason: '申请全部解押',
        status: 'released',
        application_date: new Date('2024-02-26 11:34:16'),
        process_date: new Date('2024-02-26 12:00:00'),
        complete_date: new Date('2024-02-26 12:30:00'),
        processor_id: adminUser.id,
        processor_name: adminUser.real_name || adminUser.username,
        process_comment: '审核通过，同意解押',
        remark: '客户按时还款'
      },
      {
        application_number: '20240223140542290',
        contract_id: null,
        customer_name: '张洪彬',
        customer_phone: '13700137000',
        customer_id_card: '511123199003030003',
        farmer_name: '张洪彬',
        product_name: '惠农贷',
        collateral_type: 'livestock',
        collateral_description: '优质肉牛',
        collateral_value: 500000.00,
        loan_amount: 300000.00,
        release_amount: 1000000.00,
        release_quantity: '10头',
        application_reason: '扩大养殖规模，申请解押',
        status: 'released',
        application_date: new Date('2024-02-23 14:05:42'),
        process_date: new Date('2024-02-23 15:00:00'),
        complete_date: new Date('2024-02-23 15:30:00'),
        processor_id: adminUser.id,
        processor_name: adminUser.real_name || adminUser.username,
        process_comment: '审核通过，同意解押',
        remark: '客户经营状况良好'
      },
      {
        application_number: '20231131890123456',
        contract_id: null,
        customer_name: '田小平',
        customer_phone: '13600136000',
        customer_id_card: '150123199001013140',
        farmer_name: '田小平',
        product_name: '中国工商银行扎旗支行"畜禽活体抵押"',
        collateral_type: 'livestock',
        collateral_description: '优质肉牛',
        collateral_value: 600000.00,
        loan_amount: 400000.00,
        release_amount: 420000.00,
        release_quantity: '30头',
        application_reason: '需要资金购买饲料',
        status: 'released',
        application_date: new Date('2023-11-31 08:30:12'),
        process_date: new Date('2023-11-31 10:00:00'),
        complete_date: new Date('2023-11-31 10:30:00'),
        processor_id: adminUser.id,
        processor_name: adminUser.real_name || adminUser.username,
        process_comment: '审核通过，同意解押',
        remark: '客户信用记录良好'
      },
      {
        application_number: '20231131789012345',
        contract_id: null,
        customer_name: '杜宝民',
        customer_phone: '13500135000',
        customer_id_card: '150123199002020723',
        farmer_name: '杜宝民',
        product_name: '中国农业银行扎旗支行"畜禽活体抵押"',
        collateral_type: 'livestock',
        collateral_description: '优质奶牛',
        collateral_value: 700000.00,
        loan_amount: 500000.00,
        release_amount: 420000.00,
        release_quantity: '30头',
        application_reason: '设备更新需要资金',
        status: 'released',
        application_date: new Date('2023-11-31 07:30:01'),
        process_date: new Date('2023-11-31 09:00:00'),
        complete_date: new Date('2023-11-31 09:30:00'),
        processor_id: adminUser.id,
        processor_name: adminUser.real_name || adminUser.username,
        process_comment: '审核通过，同意解押',
        remark: '客户还款能力强'
      },
      {
        application_number: '20231131901234567',
        contract_id: null,
        customer_name: '满良',
        customer_phone: '13400134000',
        customer_id_card: '150123199003030514',
        farmer_name: '满良',
        product_name: '中国农业银行扎旗支行"畜禽活体抵押"',
        collateral_type: 'livestock',
        collateral_description: '优质肉牛',
        collateral_value: 800000.00,
        loan_amount: 600000.00,
        release_amount: 530000.00,
        release_quantity: '38头',
        application_reason: '扩大养殖规模',
        status: 'released',
        application_date: new Date('2023-11-31 09:01:23'),
        process_date: new Date('2023-11-31 11:00:00'),
        complete_date: new Date('2023-11-31 11:30:00'),
        processor_id: adminUser.id,
        processor_name: adminUser.real_name || adminUser.username,
        process_comment: '审核通过，同意解押',
        remark: '客户经营稳定'
      }
    ];

    // 批量创建解押申请
    const createdReleases = await LoanRelease.bulkCreate(releases);
    console.log(`✅ 创建了 ${createdReleases.length} 条解押申请记录`);

    // 为每个解押申请创建历史记录
    for (let i = 0; i < createdReleases.length; i++) {
      const release = createdReleases[i];
      
      // 提交申请历史
      await LoanReleaseHistory.create({
        release_id: release.id,
        action: 'apply',
        operator: release.customer_name,
        comment: '提交解押申请'
      });

      // 处理完成历史
      await LoanReleaseHistory.create({
        release_id: release.id,
        action: 'complete',
        operator: release.processor_name,
        operator_id: release.processor_id,
        comment: '解押手续办理完成',
        previous_status: 'pending',
        new_status: 'released'
      });
    }

    console.log(`✅ 创建了 ${createdReleases.length * 2} 条历史记录`);

    console.log('\n🎉 贷款解押测试数据添加完成！');

  } catch (error) {
    console.error('❌ 添加测试数据失败:', error.message);
    console.error('错误详情:', error);
  }
}

addTestData();
