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

provider_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"oidc",


"saml2",


"ldap"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

issuer_url: {
        type: DataTypes.TEXT,
      
      

      },

client_id: {
        type: DataTypes.TEXT,
      
      

      },

sso_metadata_url: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

idp_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"disabled"

        ],

      },

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

  identity_providers.associate = (db) => {


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











    db.identity_providers.hasMany(db.user_identities, {
      as: 'user_identities_identity_provider',
      foreignKey: {
          name: 'identity_providerId',
      },
      constraints: false,
    });













































//end loop



    db.identity_providers.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return identity_providers;
};


