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

public_label: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

delivery_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"inbox_only",


"forward_to_email"

        ],

      },

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

  private_mailboxes.associate = (db) => {


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































    db.private_mailboxes.hasMany(db.messages, {
      as: 'messages_mailbox',
      foreignKey: {
          name: 'mailboxId',
      },
      constraints: false,
    });






//end loop



    db.private_mailboxes.belongsTo(db.creator_profiles, {
      as: 'owner_creator',
      foreignKey: {
        name: 'owner_creatorId',
      },
      constraints: false,
    });

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




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

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



  return private_mailboxes;
};


