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

request_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"quote",


"callback",


"information",


"appointment",


"custom_order"

        ],

      },

full_name: {
        type: DataTypes.TEXT,
      
      

      },

company_name: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

customer_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"architect",


"interior_designer",


"contractor",


"developer",


"individual",


"other"

        ],

      },

message_ar: {
        type: DataTypes.TEXT,
      
      

      },

lead_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"new",


"qualified",


"in_progress",


"quoted",


"won",


"lost",


"closed"

        ],

      },

preferred_contact_time: {
        type: DataTypes.DATE,
      
      

      },

last_contacted_at: {
        type: DataTypes.DATE,
      
      

      },

internal_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  lead_requests.associate = (db) => {


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

























    db.lead_requests.hasMany(db.quote_items, {
      as: 'quote_items_lead_request',
      foreignKey: {
          name: 'lead_requestId',
      },
      constraints: false,
    });






//end loop



    db.lead_requests.belongsTo(db.stones, {
      as: 'interested_stone',
      foreignKey: {
        name: 'interested_stoneId',
      },
      constraints: false,
    });

    db.lead_requests.belongsTo(db.services, {
      as: 'interested_service',
      foreignKey: {
        name: 'interested_serviceId',
      },
      constraints: false,
    });

    db.lead_requests.belongsTo(db.projects, {
      as: 'related_project',
      foreignKey: {
        name: 'related_projectId',
      },
      constraints: false,
    });

    db.lead_requests.belongsTo(db.users, {
      as: 'assigned_to',
      foreignKey: {
        name: 'assigned_toId',
      },
      constraints: false,
    });



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


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

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



  return lead_requests;
};


