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

equipment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"crusher",


"grinder",


"screen",


"classifier",


"pump",


"thickener",


"filter",


"flotation_cell",


"leach_tank",


"adsorption_column",


"elution",


"electrowinning",


"smelter",


"conveyor",


"reagent_system",


"other"

        ],

      },

manufacturer: {
        type: DataTypes.TEXT,
      
      

      },

model: {
        type: DataTypes.TEXT,
      
      

      },

capacity: {
        type: DataTypes.DECIMAL,
      
      

      },

capacity_unit: {
        type: DataTypes.TEXT,
      
      

      },

power_kw: {
        type: DataTypes.DECIMAL,
      
      

      },

base_capex_usd: {
        type: DataTypes.DECIMAL,
      
      

      },

install_factor: {
        type: DataTypes.DECIMAL,
      
      

      },

opex_per_unit: {
        type: DataTypes.DECIMAL,
      
      

      },

opex_unit: {
        type: DataTypes.TEXT,
      
      

      },

vendor_quote_reference: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  equipment_items.associate = (db) => {


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


















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





    db.equipment_items.hasMany(db.capex_items, {
      as: 'capex_items_equipment_item',
      foreignKey: {
          name: 'equipment_itemId',
      },
      constraints: false,
    });













//end loop



    db.equipment_items.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });



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


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

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



  return equipment_items;
};


