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: [

"call",


"email",


"meeting",


"task",


"demo",


"note"

        ],

      },

subject: {
        type: DataTypes.TEXT,
      
      

      },

details: {
        type: DataTypes.TEXT,
      
      

      },

scheduled_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"in_progress",


"completed",


"canceled",


"overdue"

        ],

      },

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

      },

reminder_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












    db.activities.hasMany(db.activity_files, {
      as: 'activity_files_activity',
      foreignKey: {
          name: 'activityId',
      },
      constraints: false,
    });







//end loop



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

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

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

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




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

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



  return activities;
};


