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

name: {
        type: DataTypes.TEXT,
      
      

      },

version_number: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"approved",


"superseded",


"archived"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

layout_json: {
        type: DataTypes.TEXT,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  flowsheets.associate = (db) => {


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


















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


    db.flowsheets.hasMany(db.flowsheet_streams, {
      as: 'flowsheet_streams_flowsheet',
      foreignKey: {
          name: 'flowsheetId',
      },
      constraints: false,
    });



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














//end loop



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

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

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

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




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

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



  return flowsheets;
};


