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

request_number: {
        type: DataTypes.TEXT,
      
      

      },

requested_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

payment_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"bank_transfer",


"mobile_money",


"check",


"cash"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"under_review",


"approved",


"rejected",


"batched",


"paid",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

justification: {
        type: DataTypes.TEXT,
      
      

      },

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

  payment_requests.associate = (db) => {


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














































    db.payment_requests.hasMany(db.payments, {
      as: 'payments_payment_request',
      foreignKey: {
          name: 'payment_requestId',
      },
      constraints: false,
    });







//end loop



    db.payment_requests.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.payment_requests.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });

    db.payment_requests.belongsTo(db.users, {
      as: 'requested_by_user',
      foreignKey: {
        name: 'requested_by_userId',
      },
      constraints: false,
    });




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

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



  return payment_requests;
};


