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

name: {
        type: DataTypes.TEXT,
      
      

      },

color_hex: {
        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,
    },
  );

  subjects.associate = (db) => {


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









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


    db.subjects.hasMany(db.pages, {
      as: 'pages_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });




    db.subjects.hasMany(db.database_entries, {
      as: 'database_entries_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });


    db.subjects.hasMany(db.sources, {
      as: 'sources_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });






    db.subjects.hasMany(db.flashcard_decks, {
      as: 'flashcard_decks_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });




    db.subjects.hasMany(db.study_sessions, {
      as: 'study_sessions_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });



    db.subjects.hasMany(db.quizzes, {
      as: 'quizzes_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });




    db.subjects.hasMany(db.tutor_conversations, {
      as: 'tutor_conversations_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });




    db.subjects.hasMany(db.study_plan_items, {
      as: 'study_plan_items_subject',
      foreignKey: {
          name: 'subjectId',
      },
      constraints: false,
    });




//end loop



    db.subjects.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

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



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


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

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



  return subjects;
};


