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

lead_name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"working",


"qualified",


"unqualified",


"converted"

        ],

      },

source: {
        type: DataTypes.ENUM,
      
      

        values: [

"website",


"referral",


"inbound_email",


"outbound",


"event",


"partner",


"other"

        ],

      },

estimated_value: {
        type: DataTypes.DECIMAL,
      
      

      },

next_follow_up_at: {
        type: DataTypes.DATE,
      
      

      },

last_contacted_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  leads.associate = (db) => {


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










    db.leads.hasMany(db.deals, {
      as: 'deals_lead',
      foreignKey: {
          name: 'leadId',
      },
      constraints: false,
    });


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





    db.leads.hasMany(db.lead_tags, {
      as: 'lead_tags_lead',
      foreignKey: {
          name: 'leadId',
      },
      constraints: false,
    });




//end loop



    db.leads.belongsTo(db.contacts, {
      as: 'primary_contact',
      foreignKey: {
        name: 'primary_contactId',
      },
      constraints: false,
    });

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

    db.leads.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });




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

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



  return leads;
};


