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

name: {
        type: DataTypes.TEXT,
      
      

      },

data_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"numeric",


"attribute"

        ],

      },

unit: {
        type: DataTypes.TEXT,
      
      

      },

lower_spec: {
        type: DataTypes.DECIMAL,
      
      

      },

upper_spec: {
        type: DataTypes.DECIMAL,
      
      

      },

result_required: {
        type: DataTypes.ENUM,
      
      

        values: [

"required",


"optional"

        ],

      },

sequence: {
        type: DataTypes.INTEGER,
      
      

      },

method: {
        type: DataTypes.TEXT,
      
      

      },

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

  qa_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.qa_characteristics.hasMany(db.qa_results, {
      as: 'qa_results_characteristic',
      foreignKey: {
          name: 'characteristicId',
      },
      constraints: false,
    });







//end loop



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

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




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

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



  return qa_characteristics;
};


