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

title_text: {
        type: DataTypes.TEXT,
      
      

      },

abstract_text: {
        type: DataTypes.TEXT,
      
      

      },

journal: {
        type: DataTypes.TEXT,
      
      

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

doi: {
        type: DataTypes.TEXT,
      
      

      },

url: {
        type: DataTypes.TEXT,
      
      

      },

keywords: {
        type: DataTypes.TEXT,
      
      

      },

validation_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"validated",


"rejected"

        ],

      },

validation_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  scientific_articles.associate = (db) => {


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

























    db.scientific_articles.hasMany(db.article_recommendations, {
      as: 'article_recommendations_article',
      foreignKey: {
          name: 'articleId',
      },
      constraints: false,
    });





//end loop



    db.scientific_articles.belongsTo(db.users, {
      as: 'validated_by_user',
      foreignKey: {
        name: 'validated_by_userId',
      },
      constraints: false,
    });

    db.scientific_articles.belongsTo(db.accounts, {
      as: 'accounts',
      foreignKey: {
        name: 'accountsId',
      },
      constraints: false,
    });



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


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

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



  return scientific_articles;
};


