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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"succeeded",


"failed",


"canceled"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

input_payload: {
        type: DataTypes.TEXT,
      
      

      },

output_payload: {
        type: DataTypes.TEXT,
      
      

      },

logs: {
        type: DataTypes.TEXT,
      
      

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  tool_runs.associate = (db) => {


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




















    db.tool_runs.hasMany(db.confirmations, {
      as: 'confirmations_tool_run',
      foreignKey: {
          name: 'tool_runId',
      },
      constraints: false,
    });












//end loop



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

    db.tool_runs.belongsTo(db.tools, {
      as: 'tool',
      foreignKey: {
        name: 'toolId',
      },
      constraints: false,
    });

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




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

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



  return tool_runs;
};


