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,
      
      

      },

module: {
        type: DataTypes.ENUM,
      
      

        values: [

"sales_crm",


"manufacturing_erp",


"event_planning"

        ],

      },

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

      },

      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.pipeline_stages, {
      as: 'pipeline_stages_pipeline',
      foreignKey: {
          name: 'pipelineId',
      },
      constraints: false,
    });


    db.pipelines.hasMany(db.leads, {
      as: 'leads_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.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      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;
};


