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_code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

source: {
        type: DataTypes.ENUM,
      
      

        values: [

"referral",


"website",


"phone",


"walk_in",


"partner",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"contacted",


"qualified",


"proposal_sent",


"won",


"lost"

        ],

      },

estimated_value: {
        type: DataTypes.DECIMAL,
      
      

      },

expected_close_at: {
        type: DataTypes.DATE,
      
      

      },

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

























//end loop



    db.leads.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      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;
};


