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

name: {
        type: DataTypes.TEXT,
      
      

      },

enforcement: {
        type: DataTypes.ENUM,
      
      

        values: [

"required",


"optional",


"fallback_allowed"

        ],

      },

min_methods_required: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

      },

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

  service_auth_policies.associate = (db) => {

    db.service_auth_policies.belongsToMany(db.authentication_methods, {
      as: 'allowed_methods',
      foreignKey: {
        name: 'service_auth_policies_allowed_methodsId',
      },
      constraints: false,
      through: 'service_auth_policiesAllowed_methodsAuthentication_methods',
    });

    db.service_auth_policies.belongsToMany(db.authentication_methods, {
      as: 'allowed_methods_filter',
      foreignKey: {
        name: 'service_auth_policies_allowed_methodsId',
      },
      constraints: false,
      through: 'service_auth_policiesAllowed_methodsAuthentication_methods',
    });


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



























    db.service_auth_policies.hasMany(db.authentication_sessions, {
      as: 'authentication_sessions_service_auth_policy',
      foreignKey: {
          name: 'service_auth_policyId',
      },
      constraints: false,
    });


















//end loop



    db.service_auth_policies.belongsTo(db.service_types, {
      as: 'service_type',
      foreignKey: {
        name: 'service_typeId',
      },
      constraints: false,
    });

    db.service_auth_policies.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return service_auth_policies;
};


