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 groups = sequelize.define(
    'groups',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name_text: {
        type: DataTypes.TEXT,
      
      

      },

description_text: {
        type: DataTypes.TEXT,
      
      

      },

privacy_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"private",


"secret"

        ],

      },

membership_approval: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"request",


"invite_only"

        ],

      },

is_verified: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

member_count: {
        type: DataTypes.INTEGER,
      
      

      },

rules_text: {
        type: DataTypes.TEXT,
      
      

      },

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

  groups.associate = (db) => {

    db.groups.belongsToMany(db.users, {
      as: 'members',
      foreignKey: {
        name: 'groups_membersId',
      },
      constraints: false,
      through: 'groupsMembersUsers',
    });

    db.groups.belongsToMany(db.users, {
      as: 'members_filter',
      foreignKey: {
        name: 'groups_membersId',
      },
      constraints: false,
      through: 'groupsMembersUsers',
    });


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









    db.groups.hasMany(db.conversations, {
      as: 'conversations_group',
      foreignKey: {
          name: 'groupId',
      },
      constraints: false,
    });




    db.groups.hasMany(db.events, {
      as: 'events_group',
      foreignKey: {
          name: 'groupId',
      },
      constraints: false,
    });



    db.groups.hasMany(db.notifications, {
      as: 'notifications_group',
      foreignKey: {
          name: 'groupId',
      },
      constraints: false,
    });









    db.groups.hasMany(db.ai_recommendations, {
      as: 'ai_recommendations_group',
      foreignKey: {
          name: 'groupId',
      },
      constraints: false,
    });






//end loop



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



    db.groups.hasMany(db.file, {
      as: 'cover_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.groups.getTableName(),
        belongsToColumn: 'cover_images',
      },
    });

    db.groups.hasMany(db.file, {
      as: 'icon_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.groups.getTableName(),
        belongsToColumn: 'icon_images',
      },
    });


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

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



  return groups;
};


