module.exports = (sequelize, DataTypes) => {
  const RequestForQuotationItem = sequelize.define('RequestForQuotationItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    rfq_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'RequestForQuotation',
        key: 'id'
      }
    },
    line_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      },
      comment: 'Preferred vendor for this item (from material master)'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'request_for_quotation_item',
    indexes: [
      {
        fields: ['rfq_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  RequestForQuotationItem.associate = function(models) {
    RequestForQuotationItem.belongsTo(models.RequestForQuotation, { foreignKey: 'rfq_id', as: 'requestForQuotation' });
    RequestForQuotationItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    RequestForQuotationItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    RequestForQuotationItem.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
  };

  return RequestForQuotationItem;
};

