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

ticket_code: {
        type: DataTypes.TEXT,
      
      

      },

price_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency_code: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"EUR",


"GBP",


"INR",


"JPY"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"reserved",


"paid",


"refunded",


"cancelled"

        ],

      },

purchased_at: {
        type: DataTypes.DATE,
      
      

      },

checked_in_at: {
        type: DataTypes.DATE,
      
      

      },

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

  tickets.associate = (db) => {


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





















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







//end loop



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

    db.tickets.belongsTo(db.users, {
      as: 'buyer',
      foreignKey: {
        name: 'buyerId',
      },
      constraints: false,
    });




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

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



  return tickets;
};


