module.exports = function (sequelize, DataTypes) {
  const ui_elements = sequelize.define(
    'ui_elements',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      element_type: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      name: {
        type: DataTypes.TEXT,
      },
      settings_json: {
        type: DataTypes.TEXT,
      },
      sort_order: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
      indexes: [{ fields: ['element_type'] }, { fields: ['sort_order'] }, { fields: ['deletedAt'] }],
    },
  );

  ui_elements.associate = (db) => {
    db.ui_elements.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.ui_elements.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };

  return ui_elements;
};
