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

name: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

lesson_number: {
        type: DataTypes.INTEGER,
      
      

      },

estimated_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

base_content: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

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

  lessons.associate = (db) => {

    db.lessons.belongsToMany(db.skill_nodes, {
      as: 'skill_nodes_awarded',
      foreignKey: {
        name: 'lessons_skill_nodes_awardedId',
      },
      constraints: false,
      through: 'lessonsSkill_nodes_awardedSkill_nodes',
    });

    db.lessons.belongsToMany(db.skill_nodes, {
      as: 'skill_nodes_awarded_filter',
      foreignKey: {
        name: 'lessons_skill_nodes_awardedId',
      },
      constraints: false,
      through: 'lessonsSkill_nodes_awardedSkill_nodes',
    });


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















    db.lessons.hasMany(db.lesson_variants, {
      as: 'lesson_variants_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });


    db.lessons.hasMany(db.quiz_questions, {
      as: 'quiz_questions_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });


    db.lessons.hasMany(db.build_tasks, {
      as: 'build_tasks_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });



    db.lessons.hasMany(db.lesson_progress, {
      as: 'lesson_progress_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });


    db.lessons.hasMany(db.quiz_attempts, {
      as: 'quiz_attempts_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });



    db.lessons.hasMany(db.ai_chat_sessions, {
      as: 'ai_chat_sessions_lesson',
      foreignKey: {
          name: 'lessonId',
      },
      constraints: false,
    });












//end loop



    db.lessons.belongsTo(db.course_phases, {
      as: 'course_phase',
      foreignKey: {
        name: 'course_phaseId',
      },
      constraints: false,
    });

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




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

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



  return lessons;
};


