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

provider: {
        type: DataTypes.ENUM,
      
      

        values: [

"helcim"

        ],

      },

provider_reference: {
        type: DataTypes.TEXT,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency_code: {
        type: DataTypes.TEXT,
      
      

      },

intent_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"requires_payment",


"processing",


"succeeded",


"failed",


"canceled"

        ],

      },

initiated_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

failure_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  payment_intents.associate = (db) => {


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






















    db.payment_intents.hasMany(db.payments, {
      as: 'payments_payment_intent',
      foreignKey: {
          name: 'payment_intentId',
      },
      constraints: false,
    });












//end loop



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

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




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

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



  return payment_intents;
};


