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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  topics.associate = (db) => {


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










    db.topics.hasMany(db.pages, {
      as: 'pages_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });




    db.topics.hasMany(db.database_entries, {
      as: 'database_entries_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });


    db.topics.hasMany(db.sources, {
      as: 'sources_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });






    db.topics.hasMany(db.flashcard_decks, {
      as: 'flashcard_decks_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });




    db.topics.hasMany(db.study_sessions, {
      as: 'study_sessions_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });



    db.topics.hasMany(db.quizzes, {
      as: 'quizzes_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });




    db.topics.hasMany(db.tutor_conversations, {
      as: 'tutor_conversations_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });




    db.topics.hasMany(db.study_plan_items, {
      as: 'study_plan_items_topic',
      foreignKey: {
          name: 'topicId',
      },
      constraints: false,
    });




//end loop



    db.topics.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

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




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

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



  return topics;
};


