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

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"open_finance",


"brokerage",


"govbr_ecac",


"sefaz_nfe",


"pdf_upload",


"manual"

        ],

      },

institution_name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"conectado",


"importando",


"sucesso",


"parcial",


"erro",


"desconectado"

        ],

      },

imported_payload_json: {
        type: DataTypes.TEXT,
      
      

      },

imported_at: {
        type: DataTypes.DATE,
      
      

      },

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

  data_sources.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity











    db.data_sources.hasMany(db.transactions, {
      as: 'transactions_data_source',
      foreignKey: {
          name: 'data_sourceId',
      },
      constraints: false,
    });





















//end loop



    db.data_sources.belongsTo(db.declarations, {
      as: 'declaration',
      foreignKey: {
        name: 'declarationId',
      },
      constraints: false,
    });

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




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

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



  return data_sources;
};


