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

reading_at: {
        type: DataTypes.DATE,
      
      

      },

metric: {
        type: DataTypes.ENUM,
      
      

        values: [

"temperature_c",


"vibration_mm_s",


"power_kw",


"pressure_bar",


"speed_rpm",


"parts_count",


"downtime_minutes"

        ],

      },

metric_value: {
        type: DataTypes.DECIMAL,
      
      

      },

source: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  machine_readings.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.machine_readings.belongsTo(db.machines, {
      as: 'machine',
      foreignKey: {
        name: 'machineId',
      },
      constraints: false,
    });




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

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



  return machine_readings;
};


