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

target_name: {
        type: DataTypes.TEXT,
      
      

      },

target_scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"company",


"user",


"team"

        ],

      },

metric: {
        type: DataTypes.ENUM,
      
      

        values: [

"revenue",


"orders_count",


"units_sold",


"new_resellers"

        ],

      },

target_value: {
        type: DataTypes.DECIMAL,
      
      

      },

achieved_value: {
        type: DataTypes.DECIMAL,
      
      

      },

period_start: {
        type: DataTypes.DATE,
      
      

      },

period_end: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"active",


"achieved",


"missed",


"cancelled"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  sales_targets.associate = (db) => {


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




























//end loop



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

    db.sales_targets.belongsTo(db.users, {
      as: 'target_user',
      foreignKey: {
        name: 'target_userId',
      },
      constraints: false,
    });

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




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

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



  return sales_targets;
};


