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

movement_at: {
        type: DataTypes.DATE,
      
      

      },

direction: {
        type: DataTypes.ENUM,
      
      

        values: [

"in",


"out"

        ],

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

method: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"bank_transfer",


"orange_money",


"wave",


"cheque",


"card",


"other"

        ],

      },

source_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"payment",


"expense",


"adjustment",


"opening_balance",


"transfer"

        ],

      },

reference: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  cash_movements.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.cash_movements.belongsTo(db.payments, {
      as: 'payment',
      foreignKey: {
        name: 'paymentId',
      },
      constraints: false,
    });

    db.cash_movements.belongsTo(db.expenses, {
      as: 'expense',
      foreignKey: {
        name: 'expenseId',
      },
      constraints: false,
    });

    db.cash_movements.belongsTo(db.users, {
      as: 'recorded_by_user',
      foreignKey: {
        name: 'recorded_by_userId',
      },
      constraints: false,
    });




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

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



  return cash_movements;
};


