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

name: {
        type: DataTypes.TEXT,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

forecast_category: {
        type: DataTypes.ENUM,
      
      

        values: [

"pipeline",


"best_case",


"commit",


"closed_won",


"closed_lost"

        ],

      },

expected_close_at: {
        type: DataTypes.DATE,
      
      

      },

next_follow_up_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

priority: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"urgent"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  deals.associate = (db) => {


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











    db.deals.hasMany(db.activities, {
      as: 'activities_deal',
      foreignKey: {
          name: 'dealId',
      },
      constraints: false,
    });



    db.deals.hasMany(db.deal_products, {
      as: 'deal_products_deal',
      foreignKey: {
          name: 'dealId',
      },
      constraints: false,
    });




    db.deals.hasMany(db.deal_tags, {
      as: 'deal_tags_deal',
      foreignKey: {
          name: 'dealId',
      },
      constraints: false,
    });



//end loop



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

    db.deals.belongsTo(db.pipeline_stages, {
      as: 'stage',
      foreignKey: {
        name: 'stageId',
      },
      constraints: false,
    });

    db.deals.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.deals.belongsTo(db.contacts, {
      as: 'primary_contact',
      foreignKey: {
        name: 'primary_contactId',
      },
      constraints: false,
    });

    db.deals.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

    db.deals.belongsTo(db.leads, {
      as: 'lead',
      foreignKey: {
        name: 'leadId',
      },
      constraints: false,
    });




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

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



  return deals;
};


