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

step_number: {
        type: DataTypes.INTEGER,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

details: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"running",


"blocked",


"done",


"failed",


"skipped"

        ],

      },

input_payload: {
        type: DataTypes.TEXT,
      
      

      },

output_payload: {
        type: DataTypes.TEXT,
      
      

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  task_steps.associate = (db) => {


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



















    db.task_steps.hasMany(db.tool_runs, {
      as: 'tool_runs_task_step',
      foreignKey: {
          name: 'task_stepId',
      },
      constraints: false,
    });













//end loop



    db.task_steps.belongsTo(db.tasks, {
      as: 'task',
      foreignKey: {
        name: 'taskId',
      },
      constraints: false,
    });

    db.task_steps.belongsTo(db.tool_actions, {
      as: 'tool_action',
      foreignKey: {
        name: 'tool_actionId',
      },
      constraints: false,
    });

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




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

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



  return task_steps;
};


