const { sequelize, LoanProduct, User } = require('../models');

async function seedLoanProducts() {
  try {
    console.log('开始添加贷款商品测试数据...');

    // 查找管理员用户
    const adminUser = await User.findOne({
      where: { username: 'admin' }
    });

    if (!adminUser) {
      console.error('未找到管理员用户，请先创建管理员用户');
      return;
    }

    const loanProducts = [
      {
        productName: '惠农贷',
        loanAmount: '50000~5000000元',
        loanTerm: 24,
        interestRate: 3.90,
        serviceArea: '内蒙古自治区:通辽市',
        servicePhone: '15004901368',
        totalCustomers: 16,
        supervisionCustomers: 11,
        completedCustomers: 5,
        onSaleStatus: true,
        productDescription: '专为农户设计的贷款产品，支持农业生产和经营',
        applicationRequirements: '1. 具有完全民事行为能力的自然人；2. 有稳定的收入来源；3. 信用记录良好',
        requiredDocuments: '身份证、户口本、收入证明、银行流水',
        approvalProcess: '申请→初审→实地调查→审批→放款',
        riskLevel: 'LOW',
        minLoanAmount: 50000,
        maxLoanAmount: 5000000,
        createdBy: adminUser.id,
        updatedBy: adminUser.id
      },
      {
        productName: '中国工商银行扎旗支行"畜禽活体抵押"',
        loanAmount: '200000~1000000元',
        loanTerm: 12,
        interestRate: 4.70,
        serviceArea: '内蒙古自治区:通辽市',
        servicePhone: '15004901368',
        totalCustomers: 10,
        supervisionCustomers: 5,
        completedCustomers: 5,
        onSaleStatus: true,
        productDescription: '以畜禽活体作为抵押物的贷款产品',
        applicationRequirements: '1. 拥有符合条件的畜禽；2. 提供养殖证明；3. 通过银行评估',
        requiredDocuments: '身份证、养殖证明、畜禽检疫证明、银行流水',
        approvalProcess: '申请→畜禽评估→抵押登记→审批→放款',
        riskLevel: 'MEDIUM',
        minLoanAmount: 200000,
        maxLoanAmount: 1000000,
        createdBy: adminUser.id,
        updatedBy: adminUser.id
      },
      {
        productName: '中国银行扎旗支行"畜禽活体抵押"',
        loanAmount: '200000~1000000元',
        loanTerm: 12,
        interestRate: 4.60,
        serviceArea: '内蒙古自治区:通辽市',
        servicePhone: '15004901368',
        totalCustomers: 2,
        supervisionCustomers: 2,
        completedCustomers: 0,
        onSaleStatus: true,
        productDescription: '中国银行推出的畜禽活体抵押贷款产品',
        applicationRequirements: '1. 符合银行信贷政策；2. 畜禽数量达到要求；3. 提供担保',
        requiredDocuments: '身份证、养殖许可证、畜禽数量证明、担保材料',
        approvalProcess: '申请→资料审核→现场调查→风险评估→审批→放款',
        riskLevel: 'MEDIUM',
        minLoanAmount: 200000,
        maxLoanAmount: 1000000,
        createdBy: adminUser.id,
        updatedBy: adminUser.id
      },
      {
        productName: '中国农业银行扎旗支行"畜禽活体抵押"',
        loanAmount: '200000~1000000元',
        loanTerm: 12,
        interestRate: 4.80,
        serviceArea: '内蒙古自治区:通辽市',
        servicePhone: '15004901368',
        totalCustomers: 26,
        supervisionCustomers: 24,
        completedCustomers: 2,
        onSaleStatus: true,
        productDescription: '农业银行专门为养殖户设计的贷款产品',
        applicationRequirements: '1. 从事养殖业满2年；2. 畜禽存栏量达标；3. 有还款能力',
        requiredDocuments: '身份证、养殖场证明、畜禽存栏证明、收入证明',
        approvalProcess: '申请→资格审核→现场勘查→风险评估→审批→放款',
        riskLevel: 'HIGH',
        minLoanAmount: 200000,
        maxLoanAmount: 1000000,
        createdBy: adminUser.id,
        updatedBy: adminUser.id
      }
    ];

    // 检查是否已存在数据
    const existingCount = await LoanProduct.count();
    if (existingCount > 0) {
      console.log(`数据库中已存在 ${existingCount} 条贷款商品数据，跳过添加`);
      return;
    }

    // 批量创建贷款商品
    await LoanProduct.bulkCreate(loanProducts);

    console.log(`成功添加 ${loanProducts.length} 条贷款商品测试数据`);
    
    // 显示添加的数据
    const createdProducts = await LoanProduct.findAll({
      attributes: ['id', 'productName', 'loanAmount', 'interestRate', 'onSaleStatus']
    });
    
    console.log('添加的贷款商品数据:');
    createdProducts.forEach(product => {
      console.log(`- ${product.productName}: ${product.loanAmount} (利率: ${product.interestRate}%, 状态: ${product.onSaleStatus ? '在售' : '停售'})`);
    });

  } catch (error) {
    console.error('添加贷款商品测试数据失败:', error);
    throw error;
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  seedLoanProducts()
    .then(() => {
      console.log('贷款商品测试数据添加完成');
      process.exit(0);
    })
    .catch((error) => {
      console.error('添加贷款商品测试数据失败:', error);
      process.exit(1);
    });
}

module.exports = seedLoanProducts;
