module.exports = (sequelize, DataTypes) => {
  const MaterialThickness = sequelize.define('MaterialThickness', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    thickness_code: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: 'Unique code for thickness (e.g., T1.0, T1.5, T2.0)'
    },
    thickness_value: {
      type: DataTypes.DECIMAL(10, 3),
      allowNull: false,
      comment: 'Thickness value in mm'
    },
    thickness_unit: {
      type: DataTypes.STRING(10),
      allowNull: false,
      defaultValue: 'mm',
      comment: 'Unit of measurement (default: mm)'
    },
    description: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Description (e.g., 1.0mm thickness, 1.5mm thickness)'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    sort_order: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'Display order in dropdowns'
    },
    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: 'material_thickness',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'thickness_code']
      },
      {
        fields: ['company_id', 'is_active']
      },
      {
        fields: ['thickness_value']
      }
    ]
  });

  MaterialThickness.associate = function(models) {
    MaterialThickness.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    MaterialThickness.hasMany(models.Material, { foreignKey: 'thickness_id', as: 'materials' });
    MaterialThickness.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    MaterialThickness.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
  };

  return MaterialThickness;
};

