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

reason: {
        type: DataTypes.ENUM,
      
      

        values: [

"breakdown",


"setup_changeover",


"material_shortage",


"quality_hold",


"planned_maintenance",


"unplanned_maintenance",


"operator_unavailable",


"utilities",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"closed"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

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

    db.downtime_events.belongsTo(db.production_orders, {
      as: 'production_order',
      foreignKey: {
        name: 'production_orderId',
      },
      constraints: false,
    });




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

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



  return downtime_events;
};


