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

title: {
        type: DataTypes.TEXT,
      
      

      },

content_markdown: {
        type: DataTypes.TEXT,
      
      

      },

current_version_number: {
        type: DataTypes.INTEGER,
      
      

      },

last_edited_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  documents.associate = (db) => {


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













    db.documents.hasMany(db.document_versions, {
      as: 'document_versions_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });


    db.documents.hasMany(db.story_document_links, {
      as: 'story_document_links_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });


    db.documents.hasMany(db.collaboration_sessions, {
      as: 'collaboration_sessions_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });


    db.documents.hasMany(db.comment_threads, {
      as: 'comment_threads_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });






    db.documents.hasMany(db.approvals, {
      as: 'approvals_document',
      foreignKey: {
          name: 'documentId',
      },
      constraints: false,
    });











//end loop



    db.documents.belongsTo(db.teams, {
      as: 'team',
      foreignKey: {
        name: 'teamId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.users, {
      as: 'author',
      foreignKey: {
        name: 'authorId',
      },
      constraints: false,
    });

    db.documents.belongsTo(db.document_folders, {
      as: 'folder',
      foreignKey: {
        name: 'folderId',
      },
      constraints: false,
    });

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




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

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



  return documents;
};


