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

paymentDate: {
        type: DataTypes.DATE,
      
      

      },

paymentMode: {
        type: DataTypes.ENUM,
      
      

        values: [

"CHECK",


"BANK_TRANSFER",


"CASH",


"PDC"

        ],

      },

checkNumber: {
        type: DataTypes.TEXT,
      
      

      },

bankName: {
        type: DataTypes.TEXT,
      
      

      },

totalAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  ap_payments.associate = (db) => {

    db.ap_payments.belongsToMany(db.ap_payment_lines, {
      as: 'lines',
      foreignKey: {
        name: 'ap_payments_linesId',
      },
      constraints: false,
      through: 'ap_paymentsLinesAp_payment_lines',
    });

    db.ap_payments.belongsToMany(db.ap_payment_lines, {
      as: 'lines_filter',
      foreignKey: {
        name: 'ap_payments_linesId',
      },
      constraints: false,
      through: 'ap_paymentsLinesAp_payment_lines',
    });


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
































    db.ap_payments.hasMany(db.ap_payment_lines, {
      as: 'ap_payment_lines_payment',
      foreignKey: {
          name: 'paymentId',
      },
      constraints: false,
    });














//end loop



    db.ap_payments.belongsTo(db.users, {
      as: 'createdByUser',
      foreignKey: {
        name: 'createdByUserId',
      },
      constraints: false,
    });

    db.ap_payments.belongsTo(db.users, {
      as: 'approvedByUser',
      foreignKey: {
        name: 'approvedByUserId',
      },
      constraints: false,
    });




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

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



  return ap_payments;
};


