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

job_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"import_products_excel",


"bulk_invoice_create",


"export_excel",


"export_pdf",


"export_zip"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"completed",


"failed"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

processed_count: {
        type: DataTypes.INTEGER,
      
      

      },

error_count: {
        type: DataTypes.INTEGER,
      
      

      },

log: {
        type: DataTypes.TEXT,
      
      

      },

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

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



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

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


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

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



  return import_jobs;
};


