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

room_number: {
        type: DataTypes.TEXT,
      
      

      },

floor: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"out_of_service",


"maintenance"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  rooms.associate = (db) => {

    db.rooms.belongsToMany(db.room_features, {
      as: 'features',
      foreignKey: {
        name: 'rooms_featuresId',
      },
      constraints: false,
      through: 'roomsFeaturesRoom_features',
    });

    db.rooms.belongsToMany(db.room_features, {
      as: 'features_filter',
      foreignKey: {
        name: 'rooms_featuresId',
      },
      constraints: false,
      through: 'roomsFeaturesRoom_features',
    });


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

























    db.rooms.hasMany(db.housekeeping_tasks, {
      as: 'housekeeping_tasks_room',
      foreignKey: {
          name: 'roomId',
      },
      constraints: false,
    });


    db.rooms.hasMany(db.service_requests, {
      as: 'service_requests_room',
      foreignKey: {
          name: 'roomId',
      },
      constraints: false,
    });





//end loop



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




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

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



  return rooms;
};


