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

title: {
        type: DataTypes.TEXT,
      
      

      },

topic: {
        type: DataTypes.ENUM,
      
      

        values: [

"freelancing",


"insurance_basics",


"finance_basics",


"tools_templates",


"platform_guides"

        ],

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

level: {
        type: DataTypes.ENUM,
      
      

        values: [

"beginner",


"intermediate",


"advanced"

        ],

      },

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

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

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

  learning_articles.associate = (db) => {

    db.learning_articles.belongsToMany(db.tags, {
      as: 'tags',
      foreignKey: {
        name: 'learning_articles_tagsId',
      },
      constraints: false,
      through: 'learning_articlesTagsTags',
    });

    db.learning_articles.belongsToMany(db.tags, {
      as: 'tags_filter',
      foreignKey: {
        name: 'learning_articles_tagsId',
      },
      constraints: false,
      through: 'learning_articlesTagsTags',
    });


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




























//end loop





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


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

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



  return learning_articles;
};


