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

name: {
        type: DataTypes.TEXT,
      
      

      },

family_story: {
        type: DataTypes.TEXT,
      
      

      },

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

  families.associate = (db) => {


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














    db.families.hasMany(db.family_memberships, {
      as: 'family_memberships_family',
      foreignKey: {
          name: 'familyId',
      },
      constraints: false,
    });






































//end loop



    db.families.belongsTo(db.locations, {
      as: 'home_location',
      foreignKey: {
        name: 'home_locationId',
      },
      constraints: false,
    });



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


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

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



  return families;
};


