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

      submitted_at: {
        type: DataTypes.DATE,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['Pending', 'Approved', 'Rejected'],
      },

      policy_result: {
        type: DataTypes.TEXT,
      },

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

  expense_reports.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.expense_reports.hasMany(db.expense_lines, {
      as: 'expense_lines_report',
      foreignKey: {
        name: 'reportId',
      },
      constraints: false,
    });

    //end loop

    db.expense_reports.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });

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

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

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

  return expense_reports;
};
