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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

short_description: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

max_occupancy: {
        type: DataTypes.INTEGER,
      
      

      },

max_adults: {
        type: DataTypes.INTEGER,
      
      

      },

max_children: {
        type: DataTypes.INTEGER,
      
      

      },

bed_count: {
        type: DataTypes.INTEGER,
      
      

      },

bed_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"single",


"double",


"queen",


"king",


"twin",


"bunk",


"sofa_bed",


"mixed"

        ],

      },

base_area_sqm: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  room_types.associate = (db) => {

    db.room_types.belongsToMany(db.amenities, {
      as: 'amenities',
      foreignKey: {
        name: 'room_types_amenitiesId',
      },
      constraints: false,
      through: 'room_typesAmenitiesAmenities',
    });

    db.room_types.belongsToMany(db.amenities, {
      as: 'amenities_filter',
      foreignKey: {
        name: 'room_types_amenitiesId',
      },
      constraints: false,
      through: 'room_typesAmenitiesAmenities',
    });


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











    db.room_types.hasMany(db.rooms, {
      as: 'rooms_room_type',
      foreignKey: {
          name: 'room_typeId',
      },
      constraints: false,
    });





    db.room_types.hasMany(db.seasonal_rates, {
      as: 'seasonal_rates_room_type',
      foreignKey: {
          name: 'room_typeId',
      },
      constraints: false,
    });


    db.room_types.hasMany(db.inventory_blocks, {
      as: 'inventory_blocks_room_type',
      foreignKey: {
          name: 'room_typeId',
      },
      constraints: false,
    });



    db.room_types.hasMany(db.bookings, {
      as: 'bookings_room_type',
      foreignKey: {
          name: 'room_typeId',
      },
      constraints: false,
    });













//end loop



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



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


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

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



  return room_types;
};


