module.exports = (sequelize, DataTypes) => {
  const UOM = sequelize.define('UOM', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    uom_code: {
      type: DataTypes.STRING(10),
      allowNull: false,
      unique: true
    },
    uom_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    uom_description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    uom_type: {
      type: DataTypes.ENUM('WEIGHT', 'LENGTH', 'VOLUME', 'AREA', 'PIECE', 'TIME', 'ENERGY', 'OTHER'),
      allowNull: true
    },
    decimal_places: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 3
    },
    is_base_uom: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'True if this is a base unit of measure'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'uom',
    indexes: [
      {
        unique: true,
        fields: ['uom_code']
      },
      {
        fields: ['status']
      },
      {
        fields: ['uom_type']
      }
    ]
  });

  UOM.associate = function(models) {
    UOM.hasMany(models.UOMConversion, { foreignKey: 'from_uom_id', as: 'fromConversions' });
    UOM.hasMany(models.UOMConversion, { foreignKey: 'to_uom_id', as: 'toConversions' });
  };

  return UOM;
};

