// models/Category.js
module.exports = (sequelize, DataTypes) => {
  const Category = sequelize.define('Category', {
  id: {
    type: DataTypes.INTEGER.UNSIGNED,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '品类名称'
  },
  level: {
    type: DataTypes.INTEGER,
    defaultValue: 1,
    comment: '层级深度'
  },
  description: {
    type: DataTypes.TEXT,
    comment: '品类描述'
  },
  parentId: {
    type: DataTypes.INTEGER.UNSIGNED,
    allowNull: true,
    comment: '父级品类ID'
  },
  order: {
    type: DataTypes.INTEGER.UNSIGNED,
    defaultValue: 0,
    comment: '排序顺序'
  },
  isActive: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: '是否启用'
  }
}, {
  tableName: 'categories',
  timestamps: true,
  indexes: [
    {
      fields: ['parentId']
    },
    {
      fields: ['name']
    }
  ]
});

// 添加associate方法
Category.associate = function(models) {
  // 自关联关系
  Category.hasMany(Category, {
    as: 'children',
    foreignKey: 'parentId',
    onDelete: 'CASCADE'
  });

  Category.belongsTo(Category, {
    as: 'parent',
    foreignKey: 'parentId'
  });
};

  return Category;
};