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

title: {
        type: DataTypes.TEXT,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

content_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"note",


"pdf",


"document",


"link",


"image",


"audio",


"video",


"code",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"ready",


"needs_review",


"archived"

        ],

      },

studied_at: {
        type: DataTypes.DATE,
      
      

      },

next_review_at: {
        type: DataTypes.DATE,
      
      

      },

estimated_retention: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  database_entries.associate = (db) => {


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

































//end loop



    db.database_entries.belongsTo(db.databases, {
      as: 'database',
      foreignKey: {
        name: 'databaseId',
      },
      constraints: false,
    });

    db.database_entries.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.database_entries.belongsTo(db.topics, {
      as: 'topic',
      foreignKey: {
        name: 'topicId',
      },
      constraints: false,
    });

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



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


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

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



  return database_entries;
};


