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

import_entity: {
        type: DataTypes.ENUM,
      
      

        values: [

"materials",


"boms",


"bom_lines",


"routings",


"operations",


"machines",


"inventory_lots",


"suppliers",


"customers"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"completed",


"failed"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

records_processed: {
        type: DataTypes.INTEGER,
      
      

      },

records_failed: {
        type: DataTypes.INTEGER,
      
      

      },

error_log: {
        type: DataTypes.TEXT,
      
      

      },

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

  csv_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.csv_import_jobs.belongsTo(db.users, {
      as: 'requested_by_user',
      foreignKey: {
        name: 'requested_by_userId',
      },
      constraints: false,
    });



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


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

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



  return csv_import_jobs;
};


