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

node_key: {
        type: DataTypes.TEXT,
      
      

      },

node_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"unit_operation",


"source",


"sink",


"junction",


"annotation"

        ],

      },

unit_operation: {
        type: DataTypes.ENUM,
      
      

        values: [

"crushing",


"sag_mill",


"ball_mill",


"hpgr",


"cyclone",


"screen",


"gravity_concentrator",


"shaking_table",


"flotation",


"cil",


"cip",


"heap_leach",


"thickener",


"filter",


"adsorption",


"elution",


"electrowinning",


"smelting",


"tailings",


"reagent_makeup",


"water_balance",


"other"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

position_x: {
        type: DataTypes.DECIMAL,
      
      

      },

position_y: {
        type: DataTypes.DECIMAL,
      
      

      },

parameters_json: {
        type: DataTypes.TEXT,
      
      

      },

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

  flowsheet_nodes.associate = (db) => {

    db.flowsheet_nodes.belongsToMany(db.metallurgical_tests, {
      as: 'test_basis',
      foreignKey: {
        name: 'flowsheet_nodes_test_basisId',
      },
      constraints: false,
      through: 'flowsheet_nodesTest_basisMetallurgical_tests',
    });

    db.flowsheet_nodes.belongsToMany(db.metallurgical_tests, {
      as: 'test_basis_filter',
      foreignKey: {
        name: 'flowsheet_nodes_test_basisId',
      },
      constraints: false,
      through: 'flowsheet_nodesTest_basisMetallurgical_tests',
    });


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



















    db.flowsheet_nodes.hasMany(db.flowsheet_streams, {
      as: 'flowsheet_streams_source_node',
      foreignKey: {
          name: 'source_nodeId',
      },
      constraints: false,
    });

    db.flowsheet_nodes.hasMany(db.flowsheet_streams, {
      as: 'flowsheet_streams_target_node',
      foreignKey: {
          name: 'target_nodeId',
      },
      constraints: false,
    });
















//end loop



    db.flowsheet_nodes.belongsTo(db.flowsheets, {
      as: 'flowsheet',
      foreignKey: {
        name: 'flowsheetId',
      },
      constraints: false,
    });

    db.flowsheet_nodes.belongsTo(db.equipment_items, {
      as: 'equipment_item',
      foreignKey: {
        name: 'equipment_itemId',
      },
      constraints: false,
    });

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




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

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



  return flowsheet_nodes;
};


