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

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

capacity: {
        type: DataTypes.INTEGER,
      
      

      },

room_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"classroom",


"lab",


"gym",


"auditorium",


"library",


"other"

        ],

      },

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


/// 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.timetable_entries, {
      as: 'timetable_entries_room',
      foreignKey: {
          name: 'roomId',
      },
      constraints: false,
    });











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



//end loop



    db.rooms.belongsTo(db.campuses, {
      as: 'campus',
      foreignKey: {
        name: 'campusId',
      },
      constraints: false,
    });




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

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



  return rooms;
};


