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

runtime: {
        type: DataTypes.ENUM,
      
      

        values: [

"node",


"python",


"java",


"dotnet",


"go",


"rust",


"php",


"ruby",


"bash",


"browser"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"succeeded",


"failed",


"canceled",


"timed_out"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

stdout: {
        type: DataTypes.TEXT,
      
      

      },

stderr: {
        type: DataTypes.TEXT,
      
      

      },

exit_code: {
        type: DataTypes.INTEGER,
      
      

      },

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

  executions.associate = (db) => {


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



























//end loop



    db.executions.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.executions.belongsTo(db.code_snippets, {
      as: 'snippet',
      foreignKey: {
        name: 'snippetId',
      },
      constraints: false,
    });

    db.executions.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

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




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

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



  return executions;
};


