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

name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

team_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"archived"

        ],

      },

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

      },

always_on_state_retention_days: {
        type: DataTypes.INTEGER,
      
      

      },

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

  teams.associate = (db) => {


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









    db.teams.hasMany(db.memberships, {
      as: 'memberships_team',
      foreignKey: {
          name: 'teamId',
      },
      constraints: false,
    });






    db.teams.hasMany(db.conference_templates, {
      as: 'conference_templates_team',
      foreignKey: {
          name: 'teamId',
      },
      constraints: false,
    });


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






























    db.teams.hasMany(db.app_installations, {
      as: 'app_installations_team',
      foreignKey: {
          name: 'teamId',
      },
      constraints: false,
    });












//end loop



    db.teams.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.teams.belongsTo(db.departments, {
      as: 'department',
      foreignKey: {
        name: 'departmentId',
      },
      constraints: false,
    });




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

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



  return teams;
};


