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

name: {
        type: DataTypes.TEXT,
      
      

      },

pipeline_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"sales",


"renewals",


"upsell",


"partnership"

        ],

      },

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

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

  pipelines.associate = (db) => {


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













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



    db.pipelines.hasMany(db.deals, {
      as: 'deals_pipeline',
      foreignKey: {
          name: 'pipelineId',
      },
      constraints: false,
    });
































//end loop



    db.pipelines.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return pipelines;
};


