/**
 * 贷款合同测试数据脚本
 * @file seed-loan-contracts.js
 * @description 为银行系统添加贷款合同测试数据
 */
const { sequelize, LoanContract, User } = require('../models');

async function seedLoanContracts() {
  try {
    console.log('开始添加贷款合同测试数据...');

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

    // 清空现有数据
    await LoanContract.destroy({ where: {} });
    console.log('✅ 清空现有贷款合同数据');

    // 创建贷款合同测试数据（参考图片中的数据结构）
    const contracts = [
      {
        contractNumber: 'HT20231131123456789',
        applicationNumber: '20231131123456789',
        productName: '中国农业银行扎旗支行"畜禽活体抵押"',
        farmerName: '敖日布仁琴',
        borrowerName: '敖日布仁琴',
        borrowerIdNumber: '150***********4856',
        assetType: '牛',
        applicationQuantity: '36头',
        amount: 500000.00,
        paidAmount: 0,
        status: 'active',
        type: 'livestock_collateral',
        term: 24,
        interestRate: 4.20,
        phone: '13800138000',
        purpose: '养殖贷款',
        remark: '畜禽活体抵押贷款',
        contractTime: new Date('2023-11-31 12:34:56'),
        disbursementTime: new Date('2023-12-01 10:00:00'),
        maturityTime: new Date('2025-12-01 10:00:00'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231201123456790',
        applicationNumber: '20231201123456790',
        productName: '中国工商银行扎旗支行"畜禽活体抵押"',
        farmerName: '张伟',
        borrowerName: '张伟',
        borrowerIdNumber: '150***********4857',
        assetType: '牛',
        applicationQuantity: '25头',
        amount: 350000.00,
        paidAmount: 50000.00,
        status: 'active',
        type: 'livestock_collateral',
        term: 18,
        interestRate: 4.50,
        phone: '13900139000',
        purpose: '扩大养殖规模',
        remark: '工商银行畜禽活体抵押',
        contractTime: new Date('2023-12-01 14:20:30'),
        disbursementTime: new Date('2023-12-02 09:30:00'),
        maturityTime: new Date('2025-06-02 09:30:00'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231202123456791',
        applicationNumber: '20231202123456791',
        productName: '惠农贷',
        farmerName: '李明',
        borrowerName: '李明',
        borrowerIdNumber: '150***********4858',
        assetType: '牛',
        applicationQuantity: '20头',
        amount: 280000.00,
        paidAmount: 0,
        status: 'pending',
        type: 'farmer_loan',
        term: 12,
        interestRate: 3.90,
        phone: '13700137000',
        purpose: '惠农贷款',
        remark: '惠农贷产品',
        contractTime: new Date('2023-12-02 16:45:12'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231203123456792',
        applicationNumber: '20231203123456792',
        productName: '中国农业银行扎旗支行"畜禽活体抵押"',
        farmerName: '王强',
        borrowerName: '王强',
        borrowerIdNumber: '150***********4859',
        assetType: '牛',
        applicationQuantity: '30头',
        amount: 420000.00,
        paidAmount: 420000.00,
        status: 'completed',
        type: 'livestock_collateral',
        term: 24,
        interestRate: 4.20,
        phone: '13600136000',
        purpose: '养殖贷款',
        remark: '已完成还款',
        contractTime: new Date('2023-12-03 11:20:45'),
        disbursementTime: new Date('2023-12-04 08:00:00'),
        maturityTime: new Date('2025-12-04 08:00:00'),
        completedTime: new Date('2024-11-15 14:30:00'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231204123456793',
        applicationNumber: '20231204123456793',
        productName: '中国工商银行扎旗支行"畜禽活体抵押"',
        farmerName: '赵敏',
        borrowerName: '赵敏',
        borrowerIdNumber: '150***********4860',
        assetType: '牛',
        applicationQuantity: '15头',
        amount: 200000.00,
        paidAmount: 0,
        status: 'defaulted',
        type: 'livestock_collateral',
        term: 18,
        interestRate: 4.50,
        phone: '13500135000',
        purpose: '养殖贷款',
        remark: '违约状态',
        contractTime: new Date('2023-12-04 13:15:30'),
        disbursementTime: new Date('2023-12-05 10:00:00'),
        maturityTime: new Date('2025-06-05 10:00:00'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231205123456794',
        applicationNumber: '20231205123456794',
        productName: '惠农贷',
        farmerName: '刘超',
        borrowerName: '刘超',
        borrowerIdNumber: '150***********4861',
        assetType: '牛',
        applicationQuantity: '22头',
        amount: 320000.00,
        paidAmount: 80000.00,
        status: 'active',
        type: 'farmer_loan',
        term: 24,
        interestRate: 3.90,
        phone: '13400134000',
        purpose: '惠农贷款',
        remark: '惠农贷产品',
        contractTime: new Date('2023-12-05 15:30:20'),
        disbursementTime: new Date('2023-12-06 09:00:00'),
        maturityTime: new Date('2025-12-06 09:00:00'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231206123456795',
        applicationNumber: '20231206123456795',
        productName: '中国农业银行扎旗支行"畜禽活体抵押"',
        farmerName: '陈华',
        borrowerName: '陈华',
        borrowerIdNumber: '150***********4862',
        assetType: '牛',
        applicationQuantity: '28头',
        amount: 380000.00,
        paidAmount: 0,
        status: 'active',
        type: 'livestock_collateral',
        term: 30,
        interestRate: 4.20,
        phone: '13300133000',
        purpose: '养殖贷款',
        remark: '长期贷款',
        contractTime: new Date('2023-12-06 10:45:15'),
        disbursementTime: new Date('2023-12-07 11:00:00'),
        maturityTime: new Date('2026-06-07 11:00:00'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231207123456796',
        applicationNumber: '20231207123456796',
        productName: '中国工商银行扎旗支行"畜禽活体抵押"',
        farmerName: '孙丽',
        borrowerName: '孙丽',
        borrowerIdNumber: '150***********4863',
        assetType: '牛',
        applicationQuantity: '18头',
        amount: 250000.00,
        paidAmount: 250000.00,
        status: 'completed',
        type: 'livestock_collateral',
        term: 12,
        interestRate: 4.50,
        phone: '13200132000',
        purpose: '养殖贷款',
        remark: '短期贷款已完成',
        contractTime: new Date('2023-12-07 14:20:10'),
        disbursementTime: new Date('2023-12-08 08:30:00'),
        maturityTime: new Date('2024-12-08 08:30:00'),
        completedTime: new Date('2024-10-15 16:45:00'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231208123456797',
        applicationNumber: '20231208123456797',
        productName: '惠农贷',
        farmerName: '周杰',
        borrowerName: '周杰',
        borrowerIdNumber: '150***********4864',
        assetType: '牛',
        applicationQuantity: '24头',
        amount: 360000.00,
        paidAmount: 0,
        status: 'cancelled',
        type: 'farmer_loan',
        term: 18,
        interestRate: 3.90,
        phone: '13100131000',
        purpose: '惠农贷款',
        remark: '已取消',
        contractTime: new Date('2023-12-08 16:10:25'),
        createdBy: adminUser.id
      },
      {
        contractNumber: 'HT20231209123456798',
        applicationNumber: '20231209123456798',
        productName: '中国农业银行扎旗支行"畜禽活体抵押"',
        farmerName: '吴刚',
        borrowerName: '吴刚',
        borrowerIdNumber: '150***********4865',
        assetType: '牛',
        applicationQuantity: '32头',
        amount: 450000.00,
        paidAmount: 150000.00,
        status: 'active',
        type: 'livestock_collateral',
        term: 36,
        interestRate: 4.20,
        phone: '13000130000',
        purpose: '养殖贷款',
        remark: '长期贷款',
        contractTime: new Date('2023-12-09 12:30:40'),
        disbursementTime: new Date('2023-12-10 10:15:00'),
        maturityTime: new Date('2026-12-10 10:15:00'),
        createdBy: adminUser.id
      }
    ];

    // 批量创建合同
    const createdContracts = await LoanContract.bulkCreate(contracts);
    console.log(`✅ 成功创建${createdContracts.length}个贷款合同`);

    console.log('\n📊 贷款合同数据统计：');
    console.log('- 已放款：6个合同');
    console.log('- 待放款：1个合同');
    console.log('- 已完成：2个合同');
    console.log('- 违约：1个合同');
    console.log('- 已取消：1个合同');
    console.log('- 总合同金额：3,410,000.00元');
    console.log('- 已还款金额：520,000.00元');
    console.log('- 剩余还款金额：2,890,000.00元');

    console.log('\n🎉 贷款合同测试数据添加完成！');
  } catch (error) {
    console.error('❌ 添加贷款合同测试数据失败:', error);
    throw error;
  }
}

// 如果直接运行此文件
if (require.main === module) {
  seedLoanContracts()
    .then(() => {
      console.log('✅ 脚本执行完成');
      process.exit(0);
    })
    .catch((error) => {
      console.error('❌ 脚本执行失败:', error);
      process.exit(1);
    });
}

module.exports = seedLoanContracts;
