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

export_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"accounts_and_analytics",


"journal_vouchers"

        ],

      },

source_connection_name: {
        type: DataTypes.TEXT,
      
      

      },

target_connection_name: {
        type: DataTypes.TEXT,
      
      

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"completed",


"failed"

        ],

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  data_export_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.data_export_jobs.belongsTo(db.businesses, {
      as: 'business',
      foreignKey: {
        name: 'businessId',
      },
      constraints: false,
    });

    db.data_export_jobs.belongsTo(db.users, {
      as: 'requested_by_user',
      foreignKey: {
        name: 'requested_by_userId',
      },
      constraints: false,
    });



    db.data_export_jobs.hasMany(db.file, {
      as: 'log_file',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.data_export_jobs.getTableName(),
        belongsToColumn: 'log_file',
      },
    });


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

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



  return data_export_jobs;
};


