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,
      },

expense_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"paid",


"rejected"

        ],

      },

expense_date: {
        type: DataTypes.DATE,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

payee: {
        type: DataTypes.TEXT,
      
      

      },

payment_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"bank_transfer",


"card",


"mobile_money"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

      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


























































//end loop



    db.expenses.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.expenses.belongsTo(db.chart_accounts, {
      as: 'expense_account',
      foreignKey: {
        name: 'expense_accountId',
      },
      constraints: false,
    });

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

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

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



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


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

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



  return expenses;
};


