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

phase_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"exploration_scoping",


"pre_feasibility",


"feasibility",


"detailed_engineering",


"construction",


"commissioning"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"not_started",


"in_progress",


"on_hold",


"completed",


"approved"

        ],

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

target_date: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

gate_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  project_phases.associate = (db) => {


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








    db.project_phases.hasMany(db.milestones, {
      as: 'milestones_phase',
      foreignKey: {
          name: 'phaseId',
      },
      constraints: false,
    });







    db.project_phases.hasMany(db.metallurgical_tests, {
      as: 'metallurgical_tests_phase',
      foreignKey: {
          name: 'phaseId',
      },
      constraints: false,
    });



    db.project_phases.hasMany(db.flowsheets, {
      as: 'flowsheets_phase',
      foreignKey: {
          name: 'phaseId',
      },
      constraints: false,
    });





    db.project_phases.hasMany(db.design_criteria_items, {
      as: 'design_criteria_items_phase',
      foreignKey: {
          name: 'phaseId',
      },
      constraints: false,
    });


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





    db.project_phases.hasMany(db.documents, {
      as: 'documents_phase',
      foreignKey: {
          name: 'phaseId',
      },
      constraints: false,
    });




    db.project_phases.hasMany(db.tasks, {
      as: 'tasks_phase',
      foreignKey: {
          name: 'phaseId',
      },
      constraints: false,
    });







//end loop



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

    db.project_phases.belongsTo(db.users, {
      as: 'approved_by',
      foreignKey: {
        name: 'approved_byId',
      },
      constraints: false,
    });

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




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

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



  return project_phases;
};


