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

entity_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"companies",


"contacts",


"leads",


"deals",


"projects",


"tasks",


"time_entries",


"invoices",


"support_tickets"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"processing",


"completed",


"failed"

        ],

      },

total_rows: {
        type: DataTypes.INTEGER,
      
      

      },

success_rows: {
        type: DataTypes.INTEGER,
      
      

      },

failed_rows: {
        type: DataTypes.INTEGER,
      
      

      },

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

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

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

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



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


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

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



  return imports;
};


