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

artifact_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"note",


"answer",


"reflection",


"assignment_upload",


"link",


"other"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

content_text: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

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

  artifacts.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.artifacts.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

    db.artifacts.belongsTo(db.lessons, {
      as: 'lesson',
      foreignKey: {
        name: 'lessonId',
      },
      constraints: false,
    });



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


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

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



  return artifacts;
};


