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

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"unspecified",


"female",


"male",


"non_binary"

        ],

      },

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,
      
      

      },

identity_document_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"none",


"passport",


"national_id",


"drivers_license"

        ],

      },

identity_document_number: {
        type: DataTypes.TEXT,
      
      

      },

identity_document_expires_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

pii_verified_at: {
        type: DataTypes.DATE,
      
      

      },

data_retention_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"scheduled_for_anonymization",


"anonymized"

        ],

      },

anonymize_after_at: {
        type: DataTypes.DATE,
      
      

      },

      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.bookings, {
      as: 'bookings_primary_guest',
      foreignKey: {
          name: 'primary_guestId',
      },
      constraints: false,
    });


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








    db.guests.hasMany(db.service_requests, {
      as: 'service_requests_reported_by',
      foreignKey: {
          name: 'reported_byId',
      },
      constraints: false,
    });





//end loop



    db.guests.belongsTo(db.users, {
      as: 'linked_user',
      foreignKey: {
        name: 'linked_userId',
      },
      constraints: false,
    });

    db.guests.belongsTo(db.gdpr_consents, {
      as: 'gdpr_consent',
      foreignKey: {
        name: 'gdpr_consentId',
      },
      constraints: false,
    });




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

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



  return guests;
};


