module.exports = (sequelize, DataTypes) => {
  const InventoryStockTransferItem = sequelize.define('InventoryStockTransferItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    transfer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'InventoryStockTransfer',
        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_stock_transfer_item',
    indexes: [
      {
        fields: ['transfer_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  InventoryStockTransferItem.associate = function(models) {
    InventoryStockTransferItem.belongsTo(models.InventoryStockTransfer, { foreignKey: 'transfer_id', as: 'transfer' });
    InventoryStockTransferItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    InventoryStockTransferItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
  };

  return InventoryStockTransferItem;
};

