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

name: {
        type: DataTypes.TEXT,
      
      

      },

asset_tag: {
        type: DataTypes.TEXT,
      
      

      },

manufacturer: {
        type: DataTypes.TEXT,
      
      

      },

model: {
        type: DataTypes.TEXT,
      
      

      },

serial_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"running",


"idle",


"down",


"setup",


"maintenance",


"offline"

        ],

      },

commissioned_on: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  machines.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

























    db.machines.hasMany(db.production_operations, {
      as: 'production_operations_machine',
      foreignKey: {
          name: 'machineId',
      },
      constraints: false,
    });








    db.machines.hasMany(db.nonconformances, {
      as: 'nonconformances_machine',
      foreignKey: {
          name: 'machineId',
      },
      constraints: false,
    });




    db.machines.hasMany(db.downtime_events, {
      as: 'downtime_events_machine',
      foreignKey: {
          name: 'machineId',
      },
      constraints: false,
    });


    db.machines.hasMany(db.machine_readings, {
      as: 'machine_readings_machine',
      foreignKey: {
          name: 'machineId',
      },
      constraints: false,
    });


    db.machines.hasMany(db.maintenance_work_orders, {
      as: 'maintenance_work_orders_machine',
      foreignKey: {
          name: 'machineId',
      },
      constraints: false,
    });






//end loop



    db.machines.belongsTo(db.work_centers, {
      as: 'work_center',
      foreignKey: {
        name: 'work_centerId',
      },
      constraints: false,
    });

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



    db.machines.hasMany(db.file, {
      as: 'documents',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.machines.getTableName(),
        belongsToColumn: 'documents',
      },
    });


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

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



  return machines;
};


