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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

visibility: {
        type: DataTypes.ENUM,
      
      

        values: [

"public",


"private"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"archived"

        ],

      },

created_at_time: {
        type: DataTypes.DATE,
      
      

      },

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

  book_clubs.associate = (db) => {


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






























    db.book_clubs.hasMany(db.book_club_memberships, {
      as: 'book_club_memberships_book_club',
      foreignKey: {
          name: 'book_clubId',
      },
      constraints: false,
    });


    db.book_clubs.hasMany(db.club_chat_messages, {
      as: 'club_chat_messages_book_club',
      foreignKey: {
          name: 'book_clubId',
      },
      constraints: false,
    });


    db.book_clubs.hasMany(db.reading_circle_events, {
      as: 'reading_circle_events_book_club',
      foreignKey: {
          name: 'book_clubId',
      },
      constraints: false,
    });








//end loop



    db.book_clubs.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



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


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

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



  return book_clubs;
};


