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

      amount_cents: {
        type: DataTypes.INTEGER,
      },

      currency: {
        type: DataTypes.TEXT,
      },

      psp_provider: {
        type: DataTypes.ENUM,

        values: ['STRIPE', 'WOMPI', 'WEP'],
      },

      psp_txn_id: {
        type: DataTypes.TEXT,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['PENDING', 'SUCCEEDED', 'FAILED', 'REFUNDED'],
      },

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

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

    //end loop

    db.payment_transactions.belongsTo(db.subscriptions, {
      as: 'subscription',
      foreignKey: {
        name: 'subscriptionId',
      },
      constraints: false,
    });

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

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

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

  return payment_transactions;
};
