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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"disabled",


"under_review"

        ],

      },

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

      },

rules_summary: {
        type: DataTypes.TEXT,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  community_modes.associate = (db) => {


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


































    db.community_modes.hasMany(db.community_mode_build_sessions, {
      as: 'community_mode_build_sessions_result_mode',
      foreignKey: {
          name: 'result_modeId',
      },
      constraints: false,
    });




//end loop



    db.community_modes.belongsTo(db.users, {
      as: 'creator',
      foreignKey: {
        name: 'creatorId',
      },
      constraints: false,
    });



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


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

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



  return community_modes;
};


