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,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

machine_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"cnc",


"laser",


"press",


"assembly",


"printer",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"available",


"in_use",


"maintenance",


"offline"

        ],

      },

last_maintenance_at: {
        type: DataTypes.DATE,
      
      

      },

next_maintenance_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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












//end loop



    db.machines.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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




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

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



  return machines;
};


