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

name: {
        type: DataTypes.TEXT,
      
      

      },

file_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"csv",


"xls",


"xlsx"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

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

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

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



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


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

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



  return import_templates;
};


