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

reference: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"feed",


"medication",


"utilities",


"transport",


"salary",


"maintenance",


"rent",


"taxes",


"equipment",


"other"

        ],

      },

expense_date: {
        type: DataTypes.DATE,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

payment_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"bank_transfer",


"orange_money",


"wave",


"cheque",


"card",


"other"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"approved",


"paid",


"rejected"

        ],

      },

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

  expenses.associate = (db) => {


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



















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









//end loop



    db.expenses.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

    db.expenses.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      constraints: false,
    });



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


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

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



  return expenses;
};


