module.exports = (sequelize, DataTypes) => {
  const MaterialSheetType = sequelize.define('MaterialSheetType', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    sheet_type_code: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: 'Unique code for sheet type (e.g., SHEET, COIL, PLATE, STRIP)'
    },
    sheet_type_name: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: 'Name of sheet type (e.g., Sheet, Coil, Plate, Strip, Roll)'
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: 'Description of sheet type'
    },
    standard_width: {
      type: DataTypes.DECIMAL(10, 3),
      allowNull: true,
      comment: 'Standard width in mm (if applicable)'
    },
    standard_length: {
      type: DataTypes.DECIMAL(10, 3),
      allowNull: true,
      comment: 'Standard length in mm (if applicable)'
    },
    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_sheet_type',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'sheet_type_code']
      },
      {
        fields: ['company_id', 'is_active']
      }
    ]
  });

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

  return MaterialSheetType;
};

