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

keyword: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

urgency: {
        type: DataTypes.ENUM,
      
      

        values: [

"EMERGENCY",


"TODAY",


"THIS_WEEK",


"FLEXIBLE"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"SUBMITTED",


"MATCHED",


"VIEWED",


"RESPONDED",


"SCHEDULED",


"COMPLETED",


"DECLINED",


"CANCELLED"

        ],

      },

contact_name: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state: {
        type: DataTypes.TEXT,
      
      

      },

zip: {
        type: DataTypes.TEXT,
      
      

      },

lat: {
        type: DataTypes.DECIMAL,
      
      

      },

lng: {
        type: DataTypes.DECIMAL,
      
      

      },

inferred_tags_json: {
        type: DataTypes.TEXT,
      
      

      },

tenant_key: {
        type: DataTypes.TEXT,
      
      

      },

created_at_ts: {
        type: DataTypes.DATE,
      
      

      },

updated_at_ts: {
        type: DataTypes.DATE,
      
      

      },

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


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


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


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


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


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






//end loop



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

    db.leads.belongsTo(db.categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });




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

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



  return leads;
};


