module.exports = (sequelize, DataTypes) => {
  const NestingStoresRequestLine = sequelize.define(
    'NestingStoresRequestLine',
    {
      id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
      nesting_stores_request_id: { type: DataTypes.INTEGER, allowNull: false },
      sheet_group_key: { type: DataTypes.STRING(200), allowNull: true },
      material_id: { type: DataTypes.INTEGER, allowNull: false },
      material_number: {
        type: DataTypes.STRING(100),
        allowNull: true,
        comment: 'Material Master number at time of nesting send (denormalized)'
      },
      thickness: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      grade_short: { type: DataTypes.STRING(120), allowNull: true },
      max_length: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      max_width: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      height: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      dimension_unit: { type: DataTypes.STRING(20), allowNull: true, defaultValue: 'mm' },
      nesting_confirmed_qty: { type: DataTypes.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
      nesting_weight: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      scrap_weight: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      scrap_percentage: { type: DataTypes.DECIMAL(10, 3), allowNull: true },
      purpose_text: { type: DataTypes.TEXT, allowNull: true },
      warehouse_code: { type: DataTypes.STRING(50), allowNull: true },
      available_at_store_qty: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      shortage_qty: { type: DataTypes.DECIMAL(18, 3), allowNull: true },
      nesting_raw_material_text: { type: DataTypes.TEXT, allowNull: true },
      quantity_uom_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'uom', key: 'id' },
        comment: 'Material Master base UOM for nesting confirmed qty'
      },
      line_status: {
        type: DataTypes.ENUM('PENDING', 'COMPLETED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      completed_at: { type: DataTypes.DATE, allowNull: true }
    },
    {
      tableName: 'nesting_stores_request_line',
      underscored: true
    }
  );

  NestingStoresRequestLine.associate = (models) => {
    NestingStoresRequestLine.belongsTo(models.NestingStoresRequest, {
      foreignKey: 'nesting_stores_request_id',
      as: 'request'
    });
    NestingStoresRequestLine.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    NestingStoresRequestLine.belongsTo(models.UOM, { foreignKey: 'quantity_uom_id', as: 'quantityUOM' });
  };

  return NestingStoresRequestLine;
};
