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

name: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"private"

        ],

      },

member_limit: {
        type: DataTypes.INTEGER,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  study_groups.associate = (db) => {


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




































    db.study_groups.hasMany(db.study_group_memberships, {
      as: 'study_group_memberships_study_group',
      foreignKey: {
          name: 'study_groupId',
      },
      constraints: false,
    });


    db.study_groups.hasMany(db.study_group_posts, {
      as: 'study_group_posts_study_group',
      foreignKey: {
          name: 'study_groupId',
      },
      constraints: false,
    });


    db.study_groups.hasMany(db.study_group_messages, {
      as: 'study_group_messages_study_group',
      foreignKey: {
          name: 'study_groupId',
      },
      constraints: false,
    });


    db.study_groups.hasMany(db.study_group_resources, {
      as: 'study_group_resources_study_group',
      foreignKey: {
          name: 'study_groupId',
      },
      constraints: false,
    });






//end loop



    db.study_groups.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

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




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

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



  return study_groups;
};


