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

mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"focus",


"learn",


"review",


"test",


"challenge",


"tutor"

        ],

      },

session_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"pomodoro",


"deep_work",


"quick_review"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

ended_at: {
        type: DataTypes.DATE,
      
      

      },

duration_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

music_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"off",


"lofi",


"ambient",


"white_noise"

        ],

      },

focus_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  study_sessions.associate = (db) => {


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























    db.study_sessions.hasMany(db.review_events, {
      as: 'review_events_session',
      foreignKey: {
          name: 'sessionId',
      },
      constraints: false,
    });




    db.study_sessions.hasMany(db.quiz_attempts, {
      as: 'quiz_attempts_session',
      foreignKey: {
          name: 'sessionId',
      },
      constraints: false,
    });








//end loop



    db.study_sessions.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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

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

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

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




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

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



  return study_sessions;
};


