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

sequence_no: {
        type: DataTypes.INTEGER,
      
      

      },

characteristic_name: {
        type: DataTypes.TEXT,
      
      

      },

data_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"numeric",


"attribute"

        ],

      },

target_value: {
        type: DataTypes.DECIMAL,
      
      

      },

lower_limit: {
        type: DataTypes.DECIMAL,
      
      

      },

upper_limit: {
        type: DataTypes.DECIMAL,
      
      

      },

unit: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

method: {
        type: DataTypes.TEXT,
      
      

      },

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

  inspection_characteristics.associate = (db) => {


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































    db.inspection_characteristics.hasMany(db.inspection_results, {
      as: 'inspection_results_characteristic',
      foreignKey: {
          name: 'characteristicId',
      },
      constraints: false,
    });







//end loop



    db.inspection_characteristics.belongsTo(db.inspection_plans, {
      as: 'inspection_plan',
      foreignKey: {
        name: 'inspection_planId',
      },
      constraints: false,
    });




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

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



  return inspection_characteristics;
};


