// seeders/20250225000000-demo-categories.js
'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    // 先清空现有数据
    await queryInterface.bulkDelete('categories', {});
    
    // 插入顶级品类（一级）
    await queryInterface.bulkInsert('categories', [
      {
        name: '蔬菜',
        level: 1,
        description: '各类蔬菜',
        order: 1,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      {
        name: '水果',
        level: 1,
        description: '各类水果',
        order: 2,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      {
        name: '谷物',
        level: 1,
        description: '各类谷物',
        order: 3,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      }
    ]);

    // 查询获取插入的顶级品类ID
    const topCategories = await queryInterface.sequelize.query(
      "SELECT id FROM categories WHERE level = 1 ORDER BY `order` ASC",
      { type: queryInterface.sequelize.QueryTypes.SELECT }
    );
    
    const vegetableId = topCategories[0].id;
    const fruitId = topCategories[1].id;
    const grainId = topCategories[2].id;

    // 插入二级品类
    await queryInterface.bulkInsert('categories', [
      // 蔬菜的子品类
      {
        name: '叶菜类',
        level: 2,
        parentId: vegetableId,
        description: '绿叶蔬菜',
        order: 1,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      {
        name: '根茎类',
        level: 2,
        parentId: vegetableId,
        description: '根茎蔬菜',
        order: 2,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      // 水果的子品类
      {
        name: '浆果类',
        level: 2,
        parentId: fruitId,
        description: '浆果水果',
        order: 1,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      {
        name: '柑橘类',
        level: 2,
        parentId: fruitId,
        description: '柑橘水果',
        order: 2,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      // 谷物的子品类
      {
        name: '稻谷类',
        level: 2,
        parentId: grainId,
        description: '稻谷类谷物',
        order: 1,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      {
        name: '麦类',
        level: 2,
        parentId: grainId,
        description: '麦类谷物',
        order: 2,
        isActive: true,
        createdAt: new Date(),
        updatedAt: new Date()
      }
    ]);

    // 不需要插入三级品类，只保留一级和二级品类

    console.log('作物品类数据插入完成！');
  },

  async down(queryInterface, Sequelize) {
    // 先删除子级数据，再删除父级数据
    await queryInterface.bulkDelete('categories', {
      level: 3
    });
    
    await queryInterface.bulkDelete('categories', {
      level: 2
    });
    
    await queryInterface.bulkDelete('categories', {
      level: 1
    });
    
    console.log('作物品类数据已回滚！');
  }
};