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

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

nationality: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state_region: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

preferences: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

loyalty_points: {
        type: DataTypes.INTEGER,
      
      

      },

marketing_opt_in: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  guests.associate = (db) => {


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













    db.guests.hasMany(db.guest_documents, {
      as: 'guest_documents_guest',
      foreignKey: {
          name: 'guestId',
      },
      constraints: false,
    });







    db.guests.hasMany(db.reservations, {
      as: 'reservations_guest',
      foreignKey: {
          name: 'guestId',
      },
      constraints: false,
    });


    db.guests.hasMany(db.reservation_guests, {
      as: 'reservation_guests_guest',
      foreignKey: {
          name: 'guestId',
      },
      constraints: false,
    });









    db.guests.hasMany(db.notifications, {
      as: 'notifications_guest',
      foreignKey: {
          name: 'guestId',
      },
      constraints: false,
    });







//end loop



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

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




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

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



  return guests;
};


