module.exports = (sequelize, DataTypes) => {
  const InventoryGoodsReceiptItem = sequelize.define('InventoryGoodsReceiptItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    movement_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'InventoryGoodsReceipt',
        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'
      }
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'inventory_goods_receipt_item',
    indexes: [
      {
        fields: ['movement_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  InventoryGoodsReceiptItem.associate = function(models) {
    InventoryGoodsReceiptItem.belongsTo(models.InventoryGoodsReceipt, { foreignKey: 'movement_id', as: 'movement' });
    InventoryGoodsReceiptItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    InventoryGoodsReceiptItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
  };

  return InventoryGoodsReceiptItem;
};

