const { Project, User } = require('../models');
const { sequelize } = require('../config/database');

// 项目测试数据
const projectsData = [
  {
    name: '张洪彬',
    status: 'completed',
    farmName: '大数据中心',
    supervisionObject: '牛',
    supervisionQuantity: 10,
    supervisionPeriod: '23天',
    supervisionAmount: 10000.00,
    startTime: '2024-02-21',
    endTime: '2024-03-15',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: 'mapleaf',
    description: '大数据中心养殖项目'
  },
  {
    name: '田小平',
    status: 'completed',
    farmName: '139****5685_养殖场',
    supervisionObject: '牛',
    supervisionQuantity: 0,
    supervisionPeriod: '0天',
    supervisionAmount: 0.00,
    startTime: '2022-12-12',
    endTime: '2023-12-12',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: '',
    description: '田小平养殖场项目'
  },
  {
    name: '杜宝民',
    status: 'completed',
    farmName: '杜宝民养殖场',
    supervisionObject: '牛',
    supervisionQuantity: 1,
    supervisionPeriod: '20天',
    supervisionAmount: 1000000.00,
    startTime: '2023-05-08',
    endTime: '2028-05-08',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: 'mapleaf',
    description: '杜宝民养殖场长期项目'
  },
  {
    name: '满良',
    status: 'completed',
    farmName: '满良养殖场',
    supervisionObject: '牛',
    supervisionQuantity: 30,
    supervisionPeriod: '365天',
    supervisionAmount: 420000.00,
    startTime: '2023-01-01',
    endTime: '2024-01-01',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: 'mapleaf',
    description: '满良养殖场年度项目'
  },
  {
    name: '敖日布仁琴',
    status: 'supervision',
    farmName: '敖日布仁琴养殖场',
    supervisionObject: '牛',
    supervisionQuantity: 38,
    supervisionPeriod: '1827天',
    supervisionAmount: 530000.00,
    startTime: '2019-01-01',
    endTime: '2024-01-01',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: 'mapleaf',
    description: '敖日布仁琴养殖场长期监管项目'
  },
  {
    name: '那顺乌日图',
    status: 'supervision',
    farmName: '那顺乌日图养殖场',
    supervisionObject: '牛',
    supervisionQuantity: 36,
    supervisionPeriod: '1827天',
    supervisionAmount: 500000.00,
    startTime: '2019-01-01',
    endTime: '2024-01-01',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: 'mapleaf',
    description: '那顺乌日图养殖场长期监管项目'
  },
  {
    name: '巴特尔',
    status: 'supervision',
    farmName: '巴特尔养殖场',
    supervisionObject: '牛',
    supervisionQuantity: 41,
    supervisionPeriod: '1827天',
    supervisionAmount: 570000.00,
    startTime: '2019-01-01',
    endTime: '2024-01-01',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: 'mapleaf',
    description: '巴特尔养殖场长期监管项目'
  },
  {
    name: '王五',
    status: 'completed',
    farmName: '王五养殖场',
    supervisionObject: '牛',
    supervisionQuantity: 50,
    supervisionPeriod: '365天',
    supervisionAmount: 700000.00,
    startTime: '2023-01-01',
    endTime: '2024-01-01',
    earTag: 0,
    collar: 0,
    host: 0,
    loanOfficer: 'mapleaf',
    description: '王五养殖场年度项目'
  }
];

async function seedProjects() {
  try {
    console.log('开始创建项目测试数据...');
    
    // 测试数据库连接
    await sequelize.authenticate();
    console.log('✅ 数据库连接成功');
    
    // 查找管理员用户作为创建人
    const adminUser = await User.findOne({ where: { username: 'admin' } });
    const createdBy = adminUser ? adminUser.id : null;
    
    // 清空现有项目数据
    await Project.destroy({ where: {} });
    console.log('✅ 清空现有项目数据');
    
    // 创建项目数据
    for (const projectData of projectsData) {
      await Project.create({
        ...projectData,
        createdBy: createdBy,
        updatedBy: createdBy
      });
    }
    
    console.log(`✅ 成功创建 ${projectsData.length} 个项目`);
    
    // 验证数据
    const projectCount = await Project.count();
    console.log(`📊 数据库中现有项目数量: ${projectCount}`);
    
    // 显示统计信息
    const supervisionCount = await Project.count({ where: { status: 'supervision' } });
    const completedCount = await Project.count({ where: { status: 'completed' } });
    const totalAmount = await Project.sum('supervisionAmount');
    const totalQuantity = await Project.sum('supervisionQuantity');
    
    console.log('\n📈 项目统计信息:');
    console.log(`   监管中项目: ${supervisionCount}`);
    console.log(`   已结项项目: ${completedCount}`);
    console.log(`   总监管金额: ${totalAmount?.toFixed(2) || 0} 元`);
    console.log(`   总监管数量: ${totalQuantity || 0} 头`);
    
    console.log('\n🎉 项目数据创建完成！');
    
  } catch (error) {
    console.error('❌ 创建项目数据失败:', error);
    throw error;
  }
}

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

module.exports = seedProjects;
