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

provider_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"bank",


"accounting",


"csv_upload",


"manual"

        ],

      },

provider_name: {
        type: DataTypes.TEXT,
      
      

      },

connection_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"connected",


"error",


"disabled"

        ],

      },

connected_at: {
        type: DataTypes.DATE,
      
      

      },

last_sync_at: {
        type: DataTypes.DATE,
      
      

      },

external_reference: {
        type: DataTypes.TEXT,
      
      

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  data_connections.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_connections.hasMany(db.financial_accounts, {
      as: 'financial_accounts_data_connection',
      foreignKey: {
          name: 'data_connectionId',
      },
      constraints: false,
    });




    db.data_connections.hasMany(db.transactions, {
      as: 'transactions_data_connection',
      foreignKey: {
          name: 'data_connectionId',
      },
      constraints: false,
    });














    db.data_connections.hasMany(db.sync_jobs, {
      as: 'sync_jobs_data_connection',
      foreignKey: {
          name: 'data_connectionId',
      },
      constraints: false,
    });



//end loop



    db.data_connections.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

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




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

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



  return data_connections;
};


