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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"archived"

        ],

      },

default_deadline_hours: {
        type: DataTypes.INTEGER,
      
      

      },

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

  workflow_definitions.associate = (db) => {


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
















    db.workflow_definitions.hasMany(db.workflow_steps, {
      as: 'workflow_steps_workflow_definition',
      foreignKey: {
          name: 'workflow_definitionId',
      },
      constraints: false,
    });





    db.workflow_definitions.hasMany(db.contract_folders, {
      as: 'contract_folders_workflow_definition',
      foreignKey: {
          name: 'workflow_definitionId',
      },
      constraints: false,
    });

























//end loop



    db.workflow_definitions.belongsTo(db.service_types, {
      as: 'service_type',
      foreignKey: {
        name: 'service_typeId',
      },
      constraints: false,
    });

    db.workflow_definitions.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return workflow_definitions;
};


