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,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

preferred_contact_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"whatsapp",


"phone",


"email"

        ],

      },

message: {
        type: DataTypes.TEXT,
      
      

      },

source: {
        type: DataTypes.ENUM,
      
      

        values: [

"website_form",


"whatsapp_click",


"phone_click",


"email_click",


"manual"

        ],

      },

stage: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"contacted",


"qualified",


"proposal_sent",


"won",


"lost"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

follow_up_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

      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
















//end loop



    db.leads.belongsTo(db.services, {
      as: 'service',
      foreignKey: {
        name: 'serviceId',
      },
      constraints: false,
    });

    db.leads.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

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



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


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

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



  return leads;
};


