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

group_name: {
        type: DataTypes.TEXT,
      
      

      },

invite_code: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  family_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.family_groups.hasMany(db.family_members, {
      as: 'family_members_family_group',
      foreignKey: {
          name: 'family_groupId',
      },
      constraints: false,
    });


    db.family_groups.hasMany(db.sharing_permissions, {
      as: 'sharing_permissions_family_group',
      foreignKey: {
          name: 'family_groupId',
      },
      constraints: false,
    });

















































//end loop



    db.family_groups.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });




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

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



  return family_groups;
};


