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

name: {
        type: DataTypes.TEXT,
      
      

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

win_probability: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

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

  deal_stages.associate = (db) => {


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













    db.deal_stages.hasMany(db.deals, {
      as: 'deals_stage',
      foreignKey: {
          name: 'stageId',
      },
      constraints: false,
    });


















//end loop



    db.deal_stages.belongsTo(db.pipelines, {
      as: 'pipeline',
      foreignKey: {
        name: 'pipelineId',
      },
      constraints: false,
    });

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




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

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



  return deal_stages;
};


