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

entity_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"leads",


"contacts",


"companies",


"deals",


"activities",


"tasks"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"processing",


"completed",


"failed"

        ],

      },

filters_json: {
        type: DataTypes.TEXT,
      
      

      },

total_rows: {
        type: DataTypes.INTEGER,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

failure_reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  exports.associate = (db) => {


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






























//end loop



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

    db.exports.belongsTo(db.users, {
      as: 'initiated_by_user',
      foreignKey: {
        name: 'initiated_by_userId',
      },
      constraints: false,
    });

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



    db.exports.hasMany(db.file, {
      as: 'result_file',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.exports.getTableName(),
        belongsToColumn: 'result_file',
      },
    });


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

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



  return exports;
};


