const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const forum_spaces = sequelize.define(
    'forum_spaces',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

space_name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

center_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

center_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

radius_meters: {
        type: DataTypes.INTEGER,
      
      

      },

min_trust_score_to_post: {
        type: DataTypes.INTEGER,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"members_only"

        ],

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  forum_spaces.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity




























    db.forum_spaces.hasMany(db.forum_threads, {
      as: 'forum_threads_space',
      foreignKey: {
          name: 'spaceId',
      },
      constraints: false,
    });

















//end loop



    db.forum_spaces.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

    db.forum_spaces.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




    db.forum_spaces.belongsTo(db.users, {
      as: 'createdBy',
    });

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



  return forum_spaces;
};


