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,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

capacity_adults: {
        type: DataTypes.INTEGER,
      
      

      },

capacity_children: {
        type: DataTypes.INTEGER,
      
      

      },

base_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

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) => {


/// 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.dynamic_pricing_rules, {
      as: 'dynamic_pricing_rules_room_type',
      foreignKey: {
          name: 'room_typeId',
      },
      constraints: false,
    });




    db.room_types.hasMany(db.reservations, {
      as: 'reservations_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.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return room_types;
};


