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,
      },

recorded_at: {
        type: DataTypes.DATE,
      
      

      },

run_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

down_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

good_count: {
        type: DataTypes.DECIMAL,
      
      

      },

scrap_count: {
        type: DataTypes.DECIMAL,
      
      

      },

cycle_time_seconds: {
        type: DataTypes.DECIMAL,
      
      

      },

energy_kwh: {
        type: DataTypes.DECIMAL,
      
      

      },

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




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

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



  return machine_readings;
};


