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

expense_date: {
        type: DataTypes.DATE,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.TEXT,
      
      

      },

merchant: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected",


"paid"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

decision_at: {
        type: DataTypes.DATE,
      
      

      },

decision_comment: {
        type: DataTypes.TEXT,
      
      

      },

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

  expense_claims.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.expense_claims.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });

    db.expense_claims.belongsTo(db.expense_categories, {
      as: 'expense_category',
      foreignKey: {
        name: 'expense_categoryId',
      },
      constraints: false,
    });

    db.expense_claims.belongsTo(db.employees, {
      as: 'approver',
      foreignKey: {
        name: 'approverId',
      },
      constraints: false,
    });

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



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


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

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



  return expense_claims;
};


