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

activity_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"note",


"call",


"email",


"meeting",


"task",


"status_change",


"stage_change",


"system"

        ],

      },

subject: {
        type: DataTypes.TEXT,
      
      

      },

body: {
        type: DataTypes.TEXT,
      
      

      },

occurred_at: {
        type: DataTypes.DATE,
      
      

      },

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

  activities.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.activities.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.activities.belongsTo(db.users, {
      as: 'actor',
      foreignKey: {
        name: 'actorId',
      },
      constraints: false,
    });

    db.activities.belongsTo(db.leads, {
      as: 'lead',
      foreignKey: {
        name: 'leadId',
      },
      constraints: false,
    });

    db.activities.belongsTo(db.contacts, {
      as: 'contact',
      foreignKey: {
        name: 'contactId',
      },
      constraints: false,
    });

    db.activities.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.activities.belongsTo(db.deals, {
      as: 'deal',
      foreignKey: {
        name: 'dealId',
      },
      constraints: false,
    });

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



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


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

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



  return activities;
};


