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

display_name: {
        type: DataTypes.TEXT,
      
      

      },

headline: {
        type: DataTypes.TEXT,
      
      

      },

bio: {
        type: DataTypes.TEXT,
      
      

      },

location: {
        type: DataTypes.TEXT,
      
      

      },

public_slug: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  public_profiles.associate = (db) => {

    db.public_profiles.belongsToMany(db.trophies, {
      as: 'trophies',
      foreignKey: {
        name: 'public_profiles_trophiesId',
      },
      constraints: false,
      through: 'public_profilesTrophiesTrophies',
    });

    db.public_profiles.belongsToMany(db.trophies, {
      as: 'trophies_filter',
      foreignKey: {
        name: 'public_profiles_trophiesId',
      },
      constraints: false,
      through: 'public_profilesTrophiesTrophies',
    });

    db.public_profiles.belongsToMany(db.artifacts, {
      as: 'public_artifacts',
      foreignKey: {
        name: 'public_profiles_public_artifactsId',
      },
      constraints: false,
      through: 'public_profilesPublic_artifactsArtifacts',
    });

    db.public_profiles.belongsToMany(db.artifacts, {
      as: 'public_artifacts_filter',
      foreignKey: {
        name: 'public_profiles_public_artifactsId',
      },
      constraints: false,
      through: 'public_profilesPublic_artifactsArtifacts',
    });


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




























//end loop



    db.public_profiles.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });



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


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

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



  return public_profiles;
};


