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

name: {
        type: DataTypes.TEXT,
      
      

      },

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

tax_number: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

website: {
        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,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

check_in_time: {
        type: DataTypes.DATE,
      
      

      },

check_out_time: {
        type: DataTypes.DATE,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  hotels.associate = (db) => {


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










    db.hotels.hasMany(db.room_types, {
      as: 'room_types_hotel',
      foreignKey: {
          name: 'hotelId',
      },
      constraints: false,
    });





    db.hotels.hasMany(db.rate_plans, {
      as: 'rate_plans_hotel',
      foreignKey: {
          name: 'hotelId',
      },
      constraints: false,
    });





    db.hotels.hasMany(db.bookings, {
      as: 'bookings_hotel',
      foreignKey: {
          name: 'hotelId',
      },
      constraints: false,
    });






    db.hotels.hasMany(db.promo_codes, {
      as: 'promo_codes_hotel',
      foreignKey: {
          name: 'hotelId',
      },
      constraints: false,
    });





    db.hotels.hasMany(db.content_pages, {
      as: 'content_pages_hotel',
      foreignKey: {
          name: 'hotelId',
      },
      constraints: false,
    });


    db.hotels.hasMany(db.settings, {
      as: 'settings_hotel',
      foreignKey: {
          name: 'hotelId',
      },
      constraints: false,
    });



//end loop





    db.hotels.hasMany(db.file, {
      as: 'gallery_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.hotels.getTableName(),
        belongsToColumn: 'gallery_images',
      },
    });


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

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



  return hotels;
};


