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,
      },

full_name: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

company_name: {
        type: DataTypes.TEXT,
      
      

      },

job_title: {
        type: DataTypes.TEXT,
      
      

      },

lead_source: {
        type: DataTypes.ENUM,
      
      

        values: [

"website",


"referral",


"ads",


"outbound",


"event",


"partner",


"import",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"contacted",


"qualified",


"disqualified",


"converted"

        ],

      },

score: {
        type: DataTypes.DECIMAL,
      
      

      },

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.activities, {
      as: 'activities_lead',
      foreignKey: {
          name: 'leadId',
      },
      constraints: false,
    });


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
















//end loop



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

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

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

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

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




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

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



  return leads;
};


