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

payment_ref: {
        type: DataTypes.TEXT,
      
      

      },

payment_date: {
        type: DataTypes.DATE,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

allocated_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

unallocated_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

method: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"bank_transfer",


"card",


"cheque",


"wallet",


"other"

        ],

      },

reference_no: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"posted",


"voided",


"reversed"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

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

  payments.associate = (db) => {


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














    db.payments.hasMany(db.payment_allocations, {
      as: 'payment_allocations_payment',
      foreignKey: {
          name: 'paymentId',
      },
      constraints: false,
    });








    db.payments.hasMany(db.ledger_entries, {
      as: 'ledger_entries_payment',
      foreignKey: {
          name: 'paymentId',
      },
      constraints: false,
    });







//end loop



    db.payments.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.payments.belongsTo(db.drivers, {
      as: 'driver',
      foreignKey: {
        name: 'driverId',
      },
      constraints: false,
    });

    db.payments.belongsTo(db.chart_of_accounts, {
      as: 'deposit_account',
      foreignKey: {
        name: 'deposit_accountId',
      },
      constraints: false,
    });




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

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



  return payments;
};


