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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"sent",


"verified",


"failed",


"expired",


"skipped"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

provider_reference: {
        type: DataTypes.TEXT,
      
      

      },

failure_reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  authentication_attempts.associate = (db) => {


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





























    db.authentication_attempts.hasMany(db.id_verification_artifacts, {
      as: 'id_verification_artifacts_authentication_attempt',
      foreignKey: {
          name: 'authentication_attemptId',
      },
      constraints: false,
    });


    db.authentication_attempts.hasMany(db.bank_verifications, {
      as: 'bank_verifications_authentication_attempt',
      foreignKey: {
          name: 'authentication_attemptId',
      },
      constraints: false,
    });















//end loop



    db.authentication_attempts.belongsTo(db.authentication_sessions, {
      as: 'authentication_session',
      foreignKey: {
        name: 'authentication_sessionId',
      },
      constraints: false,
    });

    db.authentication_attempts.belongsTo(db.authentication_methods, {
      as: 'authentication_method',
      foreignKey: {
        name: 'authentication_methodId',
      },
      constraints: false,
    });

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




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

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



  return authentication_attempts;
};


