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

transaction_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"tip",


"subscription",


"ticket_purchase",


"withdrawal",


"refund"

        ],

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency_code: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"EUR",


"GBP",


"INR",


"JPY"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"succeeded",


"failed",


"refunded"

        ],

      },

provider_name: {
        type: DataTypes.TEXT,
      
      

      },

provider_reference: {
        type: DataTypes.TEXT,
      
      

      },

initiated_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  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.transactions.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.transactions.belongsTo(db.posts, {
      as: 'post',
      foreignKey: {
        name: 'postId',
      },
      constraints: false,
    });

    db.transactions.belongsTo(db.events, {
      as: 'event',
      foreignKey: {
        name: 'eventId',
      },
      constraints: false,
    });

    db.transactions.belongsTo(db.tickets, {
      as: 'ticket',
      foreignKey: {
        name: 'ticketId',
      },
      constraints: false,
    });




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

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



  return transactions;
};


