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,
      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"processing",


"completed",


"failed",


"canceled"

        ],

      },

original_filename: {
        type: DataTypes.TEXT,
      
      

      },

mapping_json: {
        type: DataTypes.TEXT,
      
      

      },

total_rows: {
        type: DataTypes.INTEGER,
      
      

      },

imported_rows: {
        type: DataTypes.INTEGER,
      
      

      },

failed_rows: {
        type: DataTypes.INTEGER,
      
      

      },

error_summary: {
        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,
    },
  );

  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





















    db.csv_import_jobs.hasMany(db.csv_import_row_errors, {
      as: 'csv_import_row_errors_csv_import_job',
      foreignKey: {
          name: 'csv_import_jobId',
      },
      constraints: false,
    });











//end loop



    db.csv_import_jobs.belongsTo(db.trading_accounts, {
      as: 'trading_account',
      foreignKey: {
        name: 'trading_accountId',
      },
      constraints: false,
    });



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


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

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



  return csv_import_jobs;
};


