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

title: {
        type: DataTypes.TEXT,
      
      

      },

goal: {
        type: DataTypes.TEXT,
      
      

      },

audience: {
        type: DataTypes.ENUM,
      
      

        values: [

"school",


"university",


"professional",


"general"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"archived"

        ],

      },

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

  learning_paths.associate = (db) => {

    db.learning_paths.belongsToMany(db.content_items, {
      as: 'items',
      foreignKey: {
        name: 'learning_paths_itemsId',
      },
      constraints: false,
      through: 'learning_pathsItemsContent_items',
    });

    db.learning_paths.belongsToMany(db.content_items, {
      as: 'items_filter',
      foreignKey: {
        name: 'learning_paths_itemsId',
      },
      constraints: false,
      through: 'learning_pathsItemsContent_items',
    });


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














    db.learning_paths.hasMany(db.progress_records, {
      as: 'progress_records_path',
      foreignKey: {
          name: 'pathId',
      },
      constraints: false,
    });



















//end loop



    db.learning_paths.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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



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


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

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



  return learning_paths;
};


