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

display_name: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone_e164: {
        type: DataTypes.TEXT,
      
      

      },

participant_role: {
        type: DataTypes.ENUM,
      
      

        values: [

"preparer",


"signer",


"payer",


"viewer",


"notary"

        ],

      },

party_side: {
        type: DataTypes.ENUM,
      
      

        values: [

"party_a",


"party_b",


"third_party"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"invited",


"viewed",


"payment_pending",


"auth_pending",


"ready_to_sign",


"signed",


"revoked",


"declined"

        ],

      },

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

      },

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

  contract_participants.associate = (db) => {


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
























    db.contract_participants.hasMany(db.participant_invitations, {
      as: 'participant_invitations_contract_participant',
      foreignKey: {
          name: 'contract_participantId',
      },
      constraints: false,
    });



    db.contract_participants.hasMany(db.payment_shares, {
      as: 'payment_shares_contract_participant',
      foreignKey: {
          name: 'contract_participantId',
      },
      constraints: false,
    });


    db.contract_participants.hasMany(db.authentication_sessions, {
      as: 'authentication_sessions_contract_participant',
      foreignKey: {
          name: 'contract_participantId',
      },
      constraints: false,
    });





    db.contract_participants.hasMany(db.notification_records, {
      as: 'notification_records_contract_participant',
      foreignKey: {
          name: 'contract_participantId',
      },
      constraints: false,
    });



    db.contract_participants.hasMany(db.signing_actions, {
      as: 'signing_actions_contract_participant',
      foreignKey: {
          name: 'contract_participantId',
      },
      constraints: false,
    });


    db.contract_participants.hasMany(db.document_revisions, {
      as: 'document_revisions_proposed_by_participant',
      foreignKey: {
          name: 'proposed_by_participantId',
      },
      constraints: false,
    });


    db.contract_participants.hasMany(db.revision_approvals, {
      as: 'revision_approvals_contract_participant',
      foreignKey: {
          name: 'contract_participantId',
      },
      constraints: false,
    });


    db.contract_participants.hasMany(db.revocation_requests, {
      as: 'revocation_requests_requested_by_participant',
      foreignKey: {
          name: 'requested_by_participantId',
      },
      constraints: false,
    });









//end loop



    db.contract_participants.belongsTo(db.contract_folders, {
      as: 'contract_folder',
      foreignKey: {
        name: 'contract_folderId',
      },
      constraints: false,
    });

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

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




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

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



  return contract_participants;
};


