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

event_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"status_change",


"downtime",


"maintenance",


"setup",


"changeover"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"running",


"idle",


"down",


"maintenance",


"offline"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

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

    db.machine_events.belongsTo(db.work_orders, {
      as: 'work_order',
      foreignKey: {
        name: 'work_orderId',
      },
      constraints: false,
    });

    db.machine_events.belongsTo(db.reason_codes, {
      as: 'reason_code',
      foreignKey: {
        name: 'reason_codeId',
      },
      constraints: false,
    });

    db.machine_events.belongsTo(db.users, {
      as: 'reported_by',
      foreignKey: {
        name: 'reported_byId',
      },
      constraints: false,
    });




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

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



  return machine_events;
};


