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

      workflow_name: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['pending', 'running', 'completed', 'failed'],
      },

      runtime_ms: {
        type: DataTypes.DECIMAL,
      },

      error_msg: {
        type: DataTypes.TEXT,
      },

      started_at: {
        type: DataTypes.DATE,
      },

      finished_at: {
        type: DataTypes.DATE,
      },

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

  automation_jobs.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.automation_jobs.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

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

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

  return automation_jobs;
};
