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

downtime_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"unplanned"

        ],

      },

reason_category: {
        type: DataTypes.ENUM,
      
      

        values: [

"mechanical",


"electrical",


"software",


"changeover",


"material_shortage",


"quality_hold",


"operator_unavailable",


"other"

        ],

      },

reason_detail: {
        type: DataTypes.TEXT,
      
      

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

duration_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

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

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

    db.machine_downtime_events.belongsTo(db.users, {
      as: 'reported_by_user',
      foreignKey: {
        name: 'reported_by_userId',
      },
      constraints: false,
    });

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




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

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



  return machine_downtime_events;
};


