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,
      
      

      },

room_key: {
        type: DataTypes.TEXT,
      
      

      },

room_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"meeting",


"huddle_always_on",


"classroom_course",


"breakout_parent",


"breakout_child"

        ],

      },

access_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"team_only",


"invite_only",


"lobby_required"

        ],

      },

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

      },

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

      },

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

      },

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

      },

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

      },

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

      },

last_active_at: {
        type: DataTypes.DATE,
      
      

      },

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


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









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




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










    db.rooms.hasMany(db.courses, {
      as: 'courses_default_room',
      foreignKey: {
          name: 'default_roomId',
      },
      constraints: false,
    });



















//end loop



    db.rooms.belongsTo(db.teams, {
      as: 'team',
      foreignKey: {
        name: 'teamId',
      },
      constraints: false,
    });

    db.rooms.belongsTo(db.conference_templates, {
      as: 'conference_template',
      foreignKey: {
        name: 'conference_templateId',
      },
      constraints: false,
    });

    db.rooms.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return rooms;
};


