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

job_name: {
        type: DataTypes.TEXT,
      
      

      },

target_entity: {
        type: DataTypes.ENUM,
      
      

        values: [

"items",


"customers",


"suppliers",


"chart_of_accounts",


"sales_orders",


"purchase_orders",


"invoices",


"vendor_bills"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"uploaded",


"mapping",


"processing",


"completed",


"failed"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

total_rows: {
        type: DataTypes.INTEGER,
      
      

      },

success_rows: {
        type: DataTypes.INTEGER,
      
      

      },

error_rows: {
        type: DataTypes.INTEGER,
      
      

      },

error_report: {
        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.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

    db.csv_import_jobs.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      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;
};


