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,
      },

method: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"credit_card",


"debit_card",


"bank_transfer",


"stripe"

        ],

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"authorized",


"captured",


"failed",


"refunded",


"cancelled"

        ],

      },

paid_at: {
        type: DataTypes.DATE,
      
      

      },

stripe_payment_intent_reference: {
        type: DataTypes.TEXT,
      
      

      },

stripe_charge_reference: {
        type: DataTypes.TEXT,
      
      

      },

receipt_number: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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











//end loop



    db.payments.belongsTo(db.hotels, {
      as: 'hotel',
      foreignKey: {
        name: 'hotelId',
      },
      constraints: false,
    });

    db.payments.belongsTo(db.stay_folios, {
      as: 'stay_folio',
      foreignKey: {
        name: 'stay_folioId',
      },
      constraints: false,
    });

    db.payments.belongsTo(db.reservations, {
      as: 'reservation',
      foreignKey: {
        name: 'reservationId',
      },
      constraints: false,
    });

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




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

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



  return payments;
};


