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

async function seedInstallationTasks() {
  try {
    console.log('开始创建待安装任务测试数据...')

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

    const installationTasks = [
      {
        applicationNumber: 'APP2024001',
        contractNumber: 'LOAN2024001',
        productName: '生猪养殖贷',
        customerName: '张三',
        idType: 'ID_CARD',
        idNumber: '4401XXXXXXXXXXXXXX',
        assetType: '生猪',
        equipmentToInstall: '耳标设备',
        installationStatus: 'pending',
        taskGenerationTime: new Date('2024-01-15 10:30:00'),
        completionTime: null,
        installationNotes: '需要安装耳标设备用于生猪监管',
        installerName: '李安装',
        installerPhone: '13800138001',
        installationAddress: '广东省广州市天河区某养殖场',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024002',
        contractNumber: 'LOAN2024002',
        productName: '肉牛养殖贷',
        customerName: '李四',
        idType: 'ID_CARD',
        idNumber: '4402XXXXXXXXXXXXXX',
        assetType: '肉牛',
        equipmentToInstall: '项圈设备',
        installationStatus: 'in-progress',
        taskGenerationTime: new Date('2024-01-16 14:20:00'),
        completionTime: null,
        installationNotes: '安装项圈设备用于肉牛定位监管',
        installerName: '王安装',
        installerPhone: '13800138002',
        installationAddress: '广东省深圳市南山区某养殖场',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024003',
        contractNumber: 'LOAN2024003',
        productName: '蛋鸡养殖贷',
        customerName: '王五',
        idType: 'ID_CARD',
        idNumber: '4403XXXXXXXXXXXXXX',
        assetType: '蛋鸡',
        equipmentToInstall: '监控设备',
        installationStatus: 'completed',
        taskGenerationTime: new Date('2024-01-10 09:15:00'),
        completionTime: new Date('2024-01-20 16:30:00'),
        installationNotes: '监控设备已安装完成，用于蛋鸡养殖监管',
        installerName: '赵安装',
        installerPhone: '13800138003',
        installationAddress: '广东省佛山市顺德区某养殖场',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024004',
        contractNumber: 'LOAN2024004',
        productName: '肉羊养殖贷',
        customerName: '赵六',
        idType: 'ID_CARD',
        idNumber: '4404XXXXXXXXXXXXXX',
        assetType: '肉羊',
        equipmentToInstall: '耳标设备',
        installationStatus: 'pending',
        taskGenerationTime: new Date('2024-01-18 11:45:00'),
        completionTime: null,
        installationNotes: '需要安装耳标设备用于肉羊监管',
        installerName: '钱安装',
        installerPhone: '13800138004',
        installationAddress: '广东省东莞市某养殖场',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024005',
        contractNumber: 'LOAN2024005',
        productName: '奶牛养殖贷',
        customerName: '孙七',
        idType: 'ID_CARD',
        idNumber: '4405XXXXXXXXXXXXXX',
        assetType: '奶牛',
        equipmentToInstall: '项圈设备',
        installationStatus: 'failed',
        taskGenerationTime: new Date('2024-01-12 08:30:00'),
        completionTime: null,
        installationNotes: '设备安装失败，需要重新安排安装',
        installerName: '周安装',
        installerPhone: '13800138005',
        installationAddress: '广东省中山市某养殖场',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024006',
        contractNumber: 'LOAN2024006',
        productName: '肉鸭养殖贷',
        customerName: '周八',
        idType: 'ID_CARD',
        idNumber: '4406XXXXXXXXXXXXXX',
        assetType: '肉鸭',
        equipmentToInstall: '监控设备',
        installationStatus: 'in-progress',
        taskGenerationTime: new Date('2024-01-20 15:20:00'),
        completionTime: null,
        installationNotes: '正在安装监控设备用于肉鸭养殖监管',
        installerName: '吴安装',
        installerPhone: '13800138006',
        installationAddress: '广东省江门市某养殖场',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024007',
        contractNumber: 'LOAN2024007',
        productName: '肉鸡养殖贷',
        customerName: '吴九',
        idType: 'ID_CARD',
        idNumber: '4407XXXXXXXXXXXXXX',
        assetType: '肉鸡',
        equipmentToInstall: '耳标设备',
        installationStatus: 'completed',
        taskGenerationTime: new Date('2024-01-08 13:10:00'),
        completionTime: new Date('2024-01-22 10:15:00'),
        installationNotes: '耳标设备安装完成，肉鸡监管系统正常运行',
        installerName: '郑安装',
        installerPhone: '13800138007',
        installationAddress: '广东省惠州市某养殖场',
        createdBy: user.id
      },
      {
        applicationNumber: 'APP2024008',
        contractNumber: 'LOAN2024008',
        productName: '肉猪养殖贷',
        customerName: '郑十',
        idType: 'ID_CARD',
        idNumber: '4408XXXXXXXXXXXXXX',
        assetType: '肉猪',
        equipmentToInstall: '项圈设备',
        installationStatus: 'pending',
        taskGenerationTime: new Date('2024-01-25 09:00:00'),
        completionTime: null,
        installationNotes: '待安装项圈设备用于肉猪监管',
        installerName: '冯安装',
        installerPhone: '13800138008',
        installationAddress: '广东省汕头市某养殖场',
        createdBy: user.id
      }
    ]

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

    // 批量创建待安装任务
    await InstallationTask.bulkCreate(installationTasks)

    console.log(`✅ 成功创建 ${installationTasks.length} 条待安装任务测试数据`)
    
    // 显示创建的数据
    const createdTasks = await InstallationTask.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.installationStatus}`)
    })

  } catch (error) {
    console.error('创建待安装任务测试数据失败:', error)
  }
}

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

module.exports = seedInstallationTasks
