const { CompletedSupervision, User } = require('../models')

async function seedCompletedSupervisions() {
  try {
    console.log('开始创建监管任务已结项测试数据...')

    // 获取第一个用户作为创建者
    const user = await User.findOne()
    if (!user) {
      console.error('未找到用户，请先创建用户')
      return
    }

    const completedSupervisions = [
      {
        applicationNumber: 'APP2024001',
        contractNumber: 'LOAN2024001',
        productName: '生猪养殖贷',
        customerName: '张三',
        idType: 'ID_CARD',
        idNumber: '4401XXXXXXXXXXXXXX',
        assetType: '生猪',
        assetQuantity: 500,
        totalRepaymentPeriods: 12,
        settlementStatus: 'settled',
        settlementDate: '2024-01-15',
        importTime: new Date('2024-01-15 10:30:00'),
        settlementAmount: 500000.00,
        remainingAmount: 0.00,
        settlementNotes: '贷款已全部结清',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024002',
        contractNumber: 'LOAN2024002',
        productName: '肉牛养殖贷',
        customerName: '李四',
        idType: 'ID_CARD',
        idNumber: '4402XXXXXXXXXXXXXX',
        assetType: '肉牛',
        assetQuantity: 150,
        totalRepaymentPeriods: 24,
        settlementStatus: 'partial',
        settlementDate: '2024-01-20',
        importTime: new Date('2024-01-20 14:20:00'),
        settlementAmount: 300000.00,
        remainingAmount: 200000.00,
        settlementNotes: '部分结清，剩余20万待还',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024003',
        contractNumber: 'LOAN2024003',
        productName: '蛋鸡养殖贷',
        customerName: '王五',
        idType: 'ID_CARD',
        idNumber: '4403XXXXXXXXXXXXXX',
        assetType: '蛋鸡',
        assetQuantity: 10000,
        totalRepaymentPeriods: 18,
        settlementStatus: 'unsettled',
        settlementDate: null,
        importTime: new Date('2024-01-10 09:15:00'),
        settlementAmount: null,
        remainingAmount: 800000.00,
        settlementNotes: '尚未结清',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024004',
        contractNumber: 'LOAN2024004',
        productName: '肉羊养殖贷',
        customerName: '赵六',
        idType: 'ID_CARD',
        idNumber: '4404XXXXXXXXXXXXXX',
        assetType: '肉羊',
        assetQuantity: 300,
        totalRepaymentPeriods: 15,
        settlementStatus: 'settled',
        settlementDate: '2024-01-25',
        importTime: new Date('2024-01-25 16:45:00'),
        settlementAmount: 300000.00,
        remainingAmount: 0.00,
        settlementNotes: '贷款已全部结清',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024005',
        contractNumber: 'LOAN2024005',
        productName: '奶牛养殖贷',
        customerName: '孙七',
        idType: 'ID_CARD',
        idNumber: '4405XXXXXXXXXXXXXX',
        assetType: '奶牛',
        assetQuantity: 100,
        totalRepaymentPeriods: 36,
        settlementStatus: 'partial',
        settlementDate: '2024-01-30',
        importTime: new Date('2024-01-30 11:20:00'),
        settlementAmount: 200000.00,
        remainingAmount: 400000.00,
        settlementNotes: '部分结清，剩余40万待还',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024006',
        contractNumber: 'LOAN2024006',
        productName: '肉鸭养殖贷',
        customerName: '周八',
        idType: 'ID_CARD',
        idNumber: '4406XXXXXXXXXXXXXX',
        assetType: '肉鸭',
        assetQuantity: 5000,
        totalRepaymentPeriods: 12,
        settlementStatus: 'unsettled',
        settlementDate: null,
        importTime: new Date('2024-02-01 08:30:00'),
        settlementAmount: null,
        remainingAmount: 600000.00,
        settlementNotes: '尚未结清',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024007',
        contractNumber: 'LOAN2024007',
        productName: '肉鸡养殖贷',
        customerName: '吴九',
        idType: 'ID_CARD',
        idNumber: '4407XXXXXXXXXXXXXX',
        assetType: '肉鸡',
        assetQuantity: 15000,
        totalRepaymentPeriods: 9,
        settlementStatus: 'settled',
        settlementDate: '2024-02-05',
        importTime: new Date('2024-02-05 14:15:00'),
        settlementAmount: 400000.00,
        remainingAmount: 0.00,
        settlementNotes: '贷款已全部结清',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024008',
        contractNumber: 'LOAN2024008',
        productName: '肉猪养殖贷',
        customerName: '郑十',
        idType: 'ID_CARD',
        idNumber: '4408XXXXXXXXXXXXXX',
        assetType: '肉猪',
        assetQuantity: 800,
        totalRepaymentPeriods: 18,
        settlementStatus: 'partial',
        settlementDate: '2024-02-10',
        importTime: new Date('2024-02-10 10:00:00'),
        settlementAmount: 250000.00,
        remainingAmount: 350000.00,
        settlementNotes: '部分结清，剩余35万待还',
        createdBy: user.id
      }
    ]

    // 检查是否已存在数据
    const existingCount = await CompletedSupervision.count()
    if (existingCount > 0) {
      console.log(`监管任务已结项表已有 ${existingCount} 条数据，跳过创建`)
      return
    }

    // 批量创建监管任务已结项
    await CompletedSupervision.bulkCreate(completedSupervisions)

    console.log(`✅ 成功创建 ${completedSupervisions.length} 条监管任务已结项测试数据`)
    
    // 显示创建的数据
    const createdTasks = await CompletedSupervision.findAll({
      include: [
        {
          model: User,
          as: 'creator',
          attributes: ['id', 'username', 'real_name']
        }
      ]
    })
    
    console.log('创建的监管任务已结项数据:')
    createdTasks.forEach((task, index) => {
      console.log(`${index + 1}. ${task.applicationNumber} - ${task.customerName} - ${task.settlementStatus}`)
    })

  } catch (error) {
    console.error('创建监管任务已结项测试数据失败:', error)
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  seedCompletedSupervisions()
    .then(() => {
      console.log('监管任务已结项测试数据创建完成')
      process.exit(0)
    })
    .catch((error) => {
      console.error('脚本执行失败:', error)
      process.exit(1)
    })
}

module.exports = seedCompletedSupervisions
