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

name: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

phase_number: {
        type: DataTypes.INTEGER,
      
      

      },

estimated_minutes: {
        type: DataTypes.INTEGER,
      
      

      },

unlock_rule: {
        type: DataTypes.ENUM,
      
      

        values: [

"immediate",


"after_previous_phase_complete"

        ],

      },

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

  course_phases.associate = (db) => {

    db.course_phases.belongsToMany(db.skill_nodes, {
      as: 'skills_awarded',
      foreignKey: {
        name: 'course_phases_skills_awardedId',
      },
      constraints: false,
      through: 'course_phasesSkills_awardedSkill_nodes',
    });

    db.course_phases.belongsToMany(db.skill_nodes, {
      as: 'skills_awarded_filter',
      foreignKey: {
        name: 'course_phases_skills_awardedId',
      },
      constraints: false,
      through: 'course_phasesSkills_awardedSkill_nodes',
    });


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














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




















//end loop



    db.course_phases.belongsTo(db.courses, {
      as: 'course',
      foreignKey: {
        name: 'courseId',
      },
      constraints: false,
    });

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




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

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



  return course_phases;
};


