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

posted_at: {
        type: DataTypes.DATE,
      
      

      },

effective_at: {
        type: DataTypes.DATE,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

merchant_name: {
        type: DataTypes.TEXT,
      
      

      },

direction: {
        type: DataTypes.ENUM,
      
      

        values: [

"inflow",


"outflow"

        ],

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"revenue",


"cogs",


"payroll",


"rent",


"software",


"marketing",


"travel",


"tax",


"interest",


"other_expense",


"other_income"

        ],

      },

classification_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unreviewed",


"auto_classified",


"confirmed",


"needs_attention"

        ],

      },

external_reference: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  transactions.associate = (db) => {


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


















    db.transactions.hasMany(db.anomalies, {
      as: 'anomalies_transaction',
      foreignKey: {
          name: 'transactionId',
      },
      constraints: false,
    });










//end loop



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

    db.transactions.belongsTo(db.financial_accounts, {
      as: 'financial_account',
      foreignKey: {
        name: 'financial_accountId',
      },
      constraints: false,
    });

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

    db.transactions.belongsTo(db.vendors, {
      as: 'vendor',
      foreignKey: {
        name: 'vendorId',
      },
      constraints: false,
    });

    db.transactions.belongsTo(db.customers, {
      as: 'customer',
      foreignKey: {
        name: 'customerId',
      },
      constraints: false,
    });

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




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

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



  return transactions;
};


