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

opex_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"reagents",


"power",


"labour",


"maintenance",


"consumables",


"water",


"tailings",


"general_admin",


"other"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

rate: {
        type: DataTypes.DECIMAL,
      
      

      },

rate_unit: {
        type: DataTypes.TEXT,
      
      

      },

unit_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

cost_unit: {
        type: DataTypes.TEXT,
      
      

      },

annual_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

basis_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  opex_items.associate = (db) => {

    db.opex_items.belongsToMany(db.metallurgical_tests, {
      as: 'source_tests',
      foreignKey: {
        name: 'opex_items_source_testsId',
      },
      constraints: false,
      through: 'opex_itemsSource_testsMetallurgical_tests',
    });

    db.opex_items.belongsToMany(db.metallurgical_tests, {
      as: 'source_tests_filter',
      foreignKey: {
        name: 'opex_items_source_testsId',
      },
      constraints: false,
      through: 'opex_itemsSource_testsMetallurgical_tests',
    });


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


































//end loop



    db.opex_items.belongsTo(db.economic_models, {
      as: 'economic_model',
      foreignKey: {
        name: 'economic_modelId',
      },
      constraints: false,
    });

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




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

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



  return opex_items;
};


