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,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"CDF",


"EUR",


"XAF",


"XOF"

        ],

      },

expected_close_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"won",


"lost"

        ],

      },

loss_reason: {
        type: DataTypes.TEXT,
      
      

      },

won_at: {
        type: DataTypes.DATE,
      
      

      },

lost_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        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.projects, {
      as: 'projects_deal',
      foreignKey: {
          name: 'dealId',
      },
      constraints: false,
    });






























//end loop



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

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

    db.deals.belongsTo(db.deal_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.service_lines, {
      as: 'service_line',
      foreignKey: {
        name: 'service_lineId',
      },
      constraints: false,
    });

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

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




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

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



  return deals;
};


