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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"mapping",


"validating",


"validation_failed",


"ready_to_import",


"importing",


"import_failed",


"completed",


"cancelled"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

total_rows: {
        type: DataTypes.INTEGER,
      
      

      },

valid_rows: {
        type: DataTypes.INTEGER,
      
      

      },

invalid_rows: {
        type: DataTypes.INTEGER,
      
      

      },

warning_rows: {
        type: DataTypes.INTEGER,
      
      

      },

job_name: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        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















    db.import_jobs.hasMany(db.import_row_results, {
      as: 'import_row_results_import_job',
      foreignKey: {
          name: 'import_jobId',
      },
      constraints: false,
    });










//end loop



    db.import_jobs.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.import_jobs.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

    db.import_jobs.belongsTo(db.data_entities, {
      as: 'data_entity',
      foreignKey: {
        name: 'data_entityId',
      },
      constraints: false,
    });

    db.import_jobs.belongsTo(db.file_uploads, {
      as: 'file_upload',
      foreignKey: {
        name: 'file_uploadId',
      },
      constraints: false,
    });

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



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


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

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



  return import_jobs;
};


