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: {
        type: DataTypes.INTEGER,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

data_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pass_fail",


"numeric",


"text"

        ],

      },

target_value: {
        type: DataTypes.DECIMAL,
      
      

      },

lower_spec_limit: {
        type: DataTypes.DECIMAL,
      
      

      },

upper_spec_limit: {
        type: DataTypes.DECIMAL,
      
      

      },

unit: {
        type: DataTypes.TEXT,
      
      

      },

sampling: {
        type: DataTypes.ENUM,
      
      

        values: [

"100_percent",


"aql",


"first_article",


"hourly",


"per_lot"

        ],

      },

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

      },

      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.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return inspection_characteristics;
};


