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

kpi_name: {
        type: DataTypes.TEXT,
      
      

      },

kpi_key: {
        type: DataTypes.TEXT,
      
      

      },

unit: {
        type: DataTypes.ENUM,
      
      

        values: [

"number",


"percent",


"seconds",


"minutes",


"currency"

        ],

      },

direction: {
        type: DataTypes.ENUM,
      
      

        values: [

"higher_is_better",


"lower_is_better"

        ],

      },

target_value: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  kpi_definitions.associate = (db) => {


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





































    db.kpi_definitions.hasMany(db.kpi_measurements, {
      as: 'kpi_measurements_kpi_definition',
      foreignKey: {
          name: 'kpi_definitionId',
      },
      constraints: false,
    });











//end loop



    db.kpi_definitions.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return kpi_definitions;
};


