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

event_number: {
        type: DataTypes.TEXT,
      
      

      },

reason_category: {
        type: DataTypes.ENUM,
      
      

        values: [

"breakdown",


"setup_changeover",


"material_shortage",


"quality_hold",


"operator_unavailable",


"planned_maintenance",


"unplanned_maintenance",


"other"

        ],

      },

reason_detail: {
        type: DataTypes.TEXT,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

planned: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

      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_operations, {
      as: 'production_operation',
      foreignKey: {
        name: 'production_operationId',
      },
      constraints: false,
    });

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

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




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

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



  return downtime_events;
};


