module.exports = (sequelize, DataTypes) => {
  const InProgressMaterialRequestItem = sequelize.define('InProgressMaterialRequestItem', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    in_progress_request_id: { type: DataTypes.INTEGER, allowNull: false },
    line_number: { type: DataTypes.INTEGER, allowNull: false },
    material_id: { type: DataTypes.INTEGER, allowNull: false },
    material_number: { type: DataTypes.STRING(100), allowNull: true },
    description: { type: DataTypes.STRING(500), allowNull: true },
    uom_code: { type: DataTypes.STRING(20), allowNull: true },
    store_code: { type: DataTypes.STRING(20), allowNull: true },
    required_quantity: { type: DataTypes.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
    available_quantity: { type: DataTypes.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
    shortage_quantity: { type: DataTypes.DECIMAL(18, 3), allowNull: false, defaultValue: 0 },
    status: {
      type: DataTypes.ENUM('OPEN', 'SENT_TO_MR', 'PARTIAL', 'COMPLETED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'SENT_TO_MR'
    },
    remarks: { type: DataTypes.TEXT, allowNull: true },
  }, {
    tableName: 'in_progress_material_request_item',
  });

  InProgressMaterialRequestItem.associate = function(models) {
    InProgressMaterialRequestItem.belongsTo(models.InProgressMaterialRequest, { foreignKey: 'in_progress_request_id', as: 'request' });
    InProgressMaterialRequestItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
  };

  return InProgressMaterialRequestItem;
};

