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

name: {
        type: DataTypes.TEXT,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"CAD",


"EUR",


"AUD",


"ZAR",


"BRL"

        ],

      },

gold_price: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_rate_pct: {
        type: DataTypes.DECIMAL,
      
      

      },

throughput_tpd: {
        type: DataTypes.DECIMAL,
      
      

      },

recovery_pct: {
        type: DataTypes.DECIMAL,
      
      

      },

mine_life_years: {
        type: DataTypes.DECIMAL,
      
      

      },

capex_total: {
        type: DataTypes.DECIMAL,
      
      

      },

opex_per_tonne: {
        type: DataTypes.DECIMAL,
      
      

      },

npv: {
        type: DataTypes.DECIMAL,
      
      

      },

irr_pct: {
        type: DataTypes.DECIMAL,
      
      

      },

payback_years: {
        type: DataTypes.DECIMAL,
      
      

      },

assumptions_json: {
        type: DataTypes.TEXT,
      
      

      },

sensitivity_json: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"approved",


"archived"

        ],

      },

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

  economic_models.associate = (db) => {


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






















    db.economic_models.hasMany(db.capex_items, {
      as: 'capex_items_economic_model',
      foreignKey: {
          name: 'economic_modelId',
      },
      constraints: false,
    });


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


    db.economic_models.hasMany(db.monte_carlo_runs, {
      as: 'monte_carlo_runs_economic_model',
      foreignKey: {
          name: 'economic_modelId',
      },
      constraints: false,
    });











//end loop



    db.economic_models.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.economic_models.belongsTo(db.project_phases, {
      as: 'phase',
      foreignKey: {
        name: 'phaseId',
      },
      constraints: false,
    });

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

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




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

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



  return economic_models;
};


