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_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"espece",


"orange_money",


"mtn_mobile_money",


"moov_money",


"wave",


"visa",


"mastercard"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"initie",


"reussi",


"echoue",


"rembourse"

        ],

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

paid_at: {
        type: DataTypes.DATE,
      
      

      },

provider_reference: {
        type: DataTypes.TEXT,
      
      

      },

receipt_text: {
        type: DataTypes.TEXT,
      
      

      },

points_earned: {
        type: DataTypes.INTEGER,
      
      

      },

      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.loyalty_transactions, {
      as: 'loyalty_transactions_payment',
      foreignKey: {
          name: 'paymentId',
      },
      constraints: false,
    });










//end loop



    db.payments.belongsTo(db.restaurants, {
      as: 'restaurant',
      foreignKey: {
        name: 'restaurantId',
      },
      constraints: false,
    });

    db.payments.belongsTo(db.orders, {
      as: 'order',
      foreignKey: {
        name: 'orderId',
      },
      constraints: false,
    });

    db.payments.belongsTo(db.users, {
      as: 'client_user',
      foreignKey: {
        name: 'client_userId',
      },
      constraints: false,
    });



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


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

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



  return payments;
};


