module.exports = function(sequelize, DataTypes) {
  const listing_events = sequelize.define(
    'listing_events',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      businessId: {
        type: DataTypes.UUID,
        allowNull: false,
      },
      event_type: {
        type: DataTypes.ENUM('VIEW', 'CALL_CLICK', 'WEBSITE_CLICK'),
        allowNull: false,
      },
      userId: {
        type: DataTypes.UUID,
        allowNull: true,
      },
      metadata: {
        type: DataTypes.JSONB,
        allowNull: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  listing_events.associate = (db) => {
    db.listing_events.belongsTo(db.businesses, {
      as: 'business',
      foreignKey: 'businessId',
    });

    db.listing_events.belongsTo(db.users, {
      as: 'user',
      foreignKey: 'userId',
    });
  };

  return listing_events;
};